WiFi and network connection processing developed by Android
Next, let's continue to talk about the basic use of Bluetooth in Android.
Bluetooth is one of the most widely used wireless communication protocols. It is mainly used for short-distance equipment communication (10m), and is often used to connect headphones, mice and mobile communication equipment.
It is worth mentioning that:
Android4.2 has added some new functions, but people familiar with Bluetooth may start to have a headache, that is, Android4.2 has introduced a new Bluetooth protocol stack pin BLE. Google and Broadcom cooperated to develop a new Bluetooth protocol stack to replace stack-based Bluez. Therefore, there are compatibility problems of old devices in the market, and many Bluetooth devices cannot be used normally on android4.2 phones.
BluetoothAdapter simply represents the Bluetooth adapter object of this device (mobile phone, computer, etc.). ).
First of all: we need permission.
To operate Bluetooth, you must first add permissions to AndroidManifest.xml.
* * Let's see how to use Bluetooth. **↓↓↓****
Ready for presentation:
Return value: if the device has Bluetooth function, return an instance of BluetoothAdapter; Otherwise, an empty object is returned.
How to turn on a Bluetooth device:
1. directly call the function enable () to open the Bluetooth device;
2. When the system API opens the Bluetooth device, a dialog activity will pop up for the user to choose whether to open the Bluetooth device.
Note: 1. If Bluetooth is turned on, the active interface will not pop up. 2. In most Android phones, turning on Bluetooth in airplane mode is not supported. If Bluetooth is turned on, the switch and state of Bluetooth will change with the state of flight mode.
1. Search for Bluetooth devices
Use the startDiscovery () method of BluetoothAdapter to search for Bluetooth devices.
The startDiscovery () method is an asynchronous method and will return immediately after being called. This method will search for other Bluetooth devices, and this process will last 12 seconds. After calling this method, the search process is actually carried out in a system service, so you can call the cancelDiscovery () method to stop the search (this method can be called when the discovery request is not executed).
The system starts searching for Bluetooth devices.
(* (oo)) The system will send the following three broadcasts:
2. Scanning equipment
3. Define the broadcast receiver that receives the search results.
Registered broadcast
Get nearby Bluetooth devices
The first step is to establish a connection: First, the Bluetooth connection supported by Android SDK (version 2.0 or above) is to establish a connection through BluetoothSocket. Before establishing the connection, the BluetoothServerSocket on the server side and the BluetoothSocket on the client side need to specify the same UUID. Because the method of establishing connection will block the thread, both the server and the client should start a new thread connection.
(Here, the server and the client are relative. )
The connection between two Bluetooth devices must realize the mechanism between the server and the client.
When two devices have a connected BluetoothSocket under the same RFCOMM channel, the two devices can be said to be connected.
Server devices and client devices have different ways to obtain Bluetooth sockets.
1, the server device is obtained through the incoming connection.
2. Get the client device by opening the RFCOMM channel to the server.
Computer network server
BluetoothServerSocket(UUID is used for pairing between client and server) is obtained by calling the Listenning rfcommwithServiceRecord (string, UUID) method of BluetoothAdapter.
client
Call the createrfcomSocketToServiceRecord (UUID) method of BluetoothService to get bluetooth socket(UUID should be the same as the UUID of the server).
Call the connect () method of BluetoothSocket (that is, the block method). If the UUID matches the UUID of the server and the server accepts the connection, the connect () method returns.
Data transmission, through the above operations, Bluetooth Socket connection has been established, and data transmission is nothing more than a stream.
Get stream
This category is a description of a remote Bluetooth device. Through it, you can communicate with the local Bluetooth device-BluetoothAdapter.
I don't know how to describe many things. Here's a demonstration:
Just a little friend who just learned asked me how to use ListView, so I'll use ListView.
Source code:
Rydmo
GitHub: /Rairmmd/android-demo
Code: /u/Rair/p/RairDemo/git