1: Introduction to Bluetooth Package
The Android platform provides a package that implements Bluetooth API for communication between Bluetooth devices. There are 8 classes in total, and the four commonly used classes are as follows:
BluetoothAdapter class
Represents a local Bluetooth adapter. It is the entry point for all Bluetooth interactions. Using it you can discover other Bluetooth devices, query bound devices, instantiate a Bluetooth device using a known MAC address and establish a Bluetooth ServerSocket (as the server side) to listen for connections from other devices.
BluetoothDevice class
Represents a remote Bluetooth device, which uses it to request a remote Bluetooth device to connect or obtain the name, address, type, and binding status of the remote Bluetooth device (the information is encapsulated in a BluetoothSocket).
BluetoothSocket class
Represents an interface for a Bluetooth socket (similar to sockets in TCP), which is the connection point for an application to communicate with other Bluetooth devices through input and output streams.
BlueboothServerSocket class
It opens a service connection to listen for possible incoming connection requests (belongs to the server side). In order to connect to two Bluetooth devices, one must have a device as a server to open a service socket. When the remote device initiates a connection request and has been connected to it, the BlueboothServerSocket class will return a BluetoothSocket.
Two: Use of common classes
BluetoothAdapter: Bluetooth Adapter
cancelDiscovery() cancels exploration. When we are searching for the device, we will no longer continue searching.
disable() turns off Bluetooth
enable() to turn on Bluetooth. This method will not pop up the prompt when turning on Bluetooth. More often we need to ask the user whether it is turned on. The following two lines of code also turn on Bluetooth, but will prompt the user:
Intentenabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enabler);
getAddress() gets the local Bluetooth address
getDefaultAdapter() gets the default BluetoothAdapter. In fact, there is only one method to get BluetoothAdapter.
getName() gets the local Bluetooth name
getRemoteDevice(String address) Obtain remote Bluetooth device based on Bluetooth address
getState() gets the current status of the local Bluetooth adapter
isDiscovering() determines whether the device is currently being searched, and returns true
isEnabled() determines whether Bluetooth is turned on. Return true if it is turned on, otherwise it returns false.
listenUsingRfcommWithServiceRecord(String name,UUID uuid) Create and return BluetoothServerSocket according to the name, UUID, which is the first step in creating a BluetoothSocket server side.
startDiscovery() starts searching, this is the first step in searching
BluetoothDevice: Remote Bluetooth Device
createRfcommSocketToServiceRecord(UUIDuuid) creates and returns a BluetoothSocket based on UUID. This method is also our way to get BluetoothDevice
Purpose - Create BluetoothSocket
Other methods of this class, such as getAddress(), getName(), etc., are the same as BluetoothAdapter.
BluetoothSocket: Client
// There are 6 methods in this classclose()closure connect()connect isConnected()判断是否connect getInptuStream()Get input stream getOutputStream()Get the output stream getRemoteDevice()GetBluetoothSocket指定connect的远程蓝牙设备
BluetoothServerSocket: Server
// There are 4 methods in this class
accept()
accept(int timeout)
close() close() close()
getChannel() returns the channel bound to this socket
The difference between the two is that the subsequent method specifies the outdated time. It should be noted that when these two methods are executed, the thread will be blocked until the client's request is received (or after it expires), and it should be run in the new thread. Another thing to note is that both methods return a BluetoothSocket, and the final connection is also the connection between the server and the client.
Three: Data transmission
Bluetooth data transmission--server side
1. Obtain BluetoothAdapter.
2. Create a BluetoothServerSocket object through the (name, UUID uuid) method.
3. Return a BluetoothSocket object through the () method. Since this method is in a blocking state, threads need to be turned on to handle it.
4. Obtain InputStream and OutputStream objects for reading and writing data through the () and () methods.
5. Read the data through the () method. Write data through the () method.
Bluetooth data transmission - client
1. Obtain BluetoothAdapter.
2. Obtain the BluetoothDevice object of the specified address through (String address).
3. Create a BluetoothSocket object through the (UUID uuid) method.
4. Connect to Bluetooth devices through () method.
5. Obtain InputStream and OutputStream objects for reading and writing data through the () and () methods.
6. Read the data through the () method. Write data through the () method.
Four: Tip
UUID
// UUID: Globally unique identifier, format: 8-4-4-4-12// Two Bluetooth devices need to use the same UUID to connect <uses-permissionandroid:name=".BLUETOOTH_ADMIN" /> <uses-permissionandroid:name="" />
The above is the content related to the first experience of Android Bluetooth technology shared by the editor. The next article will introduce it to you.Detailed explanation of the usage process of Android Bluetooth technology, interested friends can click to learn more.