Android Bluetooth development example analysis

When using mobile phones, Bluetooth communication brings us a lot of convenience. So how to develop Bluetooth in Android phones? This paper explains the knowledge of Android Bluetooth development by means of examples.

1. Response rights using Bluetooth

XML / HTML code

2. Configure native Bluetooth module

Here, we first need to understand a core class Bluetooth adapter for Bluetooth operation.

3. Search for Bluetooth devices

Use the startdiscovery() method of the Bluetooth adapter to search for Bluetooth devices.

The startdiscovery () method is an asynchronous method that returns immediately after the call. This method will search other Bluetooth devices for 12 seconds. After this method is called, 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).

After requesting discovery, the system starts to search for Bluetooth devices. During this process, the system will send the following three broadcasts:

ACTION_ DISCOVERY_ Start: Start Search

ACTION_ DISCOVERY_ Finished: end of search

ACTION_ Found: find the device. This intent contains two extra fields: extra_ Device and extra_ Class, including Bluetooth device and Bluetooth class respectively.

We can register the corresponding broadcastreceiver to receive the response broadcast, so as to realize some functions.

4. Bluetooth socket communication

If you intend to propose a connection between two Bluetooth devices, you must implement a server-side and client-side mechanism. When two devices have a connected Bluetooth socket under the same rfcomm channel, the two devices can be said to have established a connection.

The server device and the client device obtain Bluetooth socket in different ways. The server device is obtained by accepting an incoming connection, while the client device is obtained by opening an rfcomm channel to the server.

Server side implementation

Obtain the Bluetooth ServerSocket by calling the listenusingrfcommwithservicerecord (string, UUID) method of the Bluetooth adapter (UUID is used for pairing between the client and the server).

Call the accept () method of bluetoothserversocket to listen for connection requests. If a request is received, an instance of bluetoothsocket will be returned (this method is a block method and should be placed in a new thread).

If you do not want to accept other connections, call the close() method of bluetoothserversocket to release resources (after calling this method, the previously obtained bluetoothsocket instance is not closed. However, since rfcomm only allows one connection in one channel at a time, generally, the bluetoothserversocket is closed after accepting a connection).

Implementation of client

Search for the Bluetooth service on the server side.

Call the listenusingrfcommwithservicerecord (string, UUID) method of Bluetooth service to obtain the Bluetooth socket (the UUID should be the same as the UUID on the server side).

Call the connect () method of bluetoothsocket (this method is a block method). If the UUID matches the UUID of the server and the connection is accepted by the server, the connect () method returns.

Note: before calling the connect () method, you should make sure that there is no search device currently, otherwise the connection will become very slow and easy to fail.

5. Connection management (data communication)

Get InputStream and OutputStream respectively through getinputstream() and getoutputstream() methods of Bluetooth socket.

Use the read (bytes []) and write (bytes []) methods to read and write respectively.

Note: the read (bytes []) method will always block until the information is read from the stream, while the write (bytes []) method is not a regular block (for example, when another device does not read in time or the intermediate buffer is full, the write method will block).

The above is a simple example code for the development of Android Bluetooth. We will continue to sort out relevant materials in the future. Thank you for your support for this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>