Bluetooth data transfer problem
For Bluetooth, Google has encapsulated a lot of API so it is not very difficult to use, but the real development of Bluetooth development is not the most headache of how to invoke the API, but the interaction of data, such as long connection, data continuation, hardware acceptance rate and other issues.
There are several ways to turn on Bluetooth?
First of all, we first understand the following several commonly used open methods.
- The first method is relatively straightforward and calls the system dialog directly to start Bluetooth:
Add the required permissions in the Androidmanifest file, and the high version does not require dynamic authorization:
<uses-permission android:name="android.permission.BLUETOOTH" />//处理回调对话框startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
- The second method, silently open, will not have a dialog box for method one:
Add the required permissions in the Androidmanifest file:
- Configure the required permissions in Androidmanifest.
- For 6.0 of runtime permissions to be adapted, dynamically licensed in Java.
- Finally the direct API opens
//开启//mBluetoothAdapter.disable(); //关闭
How do I search for a Bluetooth device?
Search is divided into: Active search and passive search.
- Active Search
- Create a Bluetoothadapter object
- List of paired Bluetooth devices
- Define send receive Broadcast
What is the UUID of Bluetooth? What's the use?
UUID (Universally Unique Identifier) unified representation definition.
Bluetooth is sent through the serial port at command, bluetooth default is in the data mode, to be configured at command mode, set it, but the UUID is set before the factory.
For Bluetooth devices, each service has a UUID (unique) that corresponds to it.
Such as:
信息同步服务:00001104-0000-1000-8000-00805F9B34FB
文件传输服务:00001106-0000-1000-8000-00805F9B34FB
How do I use Bluetooth for data transfer?
The most important part of Bluetooth module communication is the data sending and receiving, which transmits data similar to the socket.
- Use sockets and ServerSocket in the network to control data read and write between client and server.
- Bluetooth communication is also done by the client and the server socket. Bluetooth client socket is bluetoothsocket, Bluetooth server socket is bluetoothserversocket. These two classes are all in the Android.bluetooth package.
- Both Bluetoothsocket and Bluetoothserversocket need a UUID (globally unique identifier, universally unique Identifier), and the UUID is the port of the socket. The Bluetooth address is equivalent to the IP of the socket.
The areas that need attention in actual development.
Need to be aware of
1. Because it involves I/O programming, it is important to note that the coded ' utf-8 ' on both ends is consistent.
2. Both the client and the server have the same UUID.
3. Bluetooth is the underlying data transfer, so the actual development is more than 16 data sent.
4. Because I/O programming is involved, it is important to use this block (synchronous, locking mechanism) for threading control.
5. For some large-capacity byte array to be sent to the attention of the place.
6. For better physical examination to avoid excessive waste of traffic, using blocking InputStream read.
1. Because it involves I/O programming, it is important to note that the coded ' utf-8 ' on both ends is consistent.
os.write("datas....".getBytes("utf-8"));
2. Both the client and the server have the same UUID.
//客户端Socketdevice.createRfcommSocketToServiceRecord(MY_UUID);//服务端SocketmBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
3. Bluetooth is the underlying data transfer, so the actual development is more than 16 data sent.
publicstaticbytesToHexString(byte[] bytes) { ""; for (int0; i < bytes.length; i++) { 0xFF); if1) { ‘0‘ + hexString; } result += hexString.toUpperCase(); } return result; }
5. For some large-capacity byte array to be sent to the attention of the place.
We need to send an array of 64 bytes, if sent in the past, the microcontroller there may not be able to deal with the timely so that there is no response, because the microcontroller there is set the delay time of data reception. It is important to consider this time difference in order to communicate with the Bluetooth module smoothly. Adjusting the send rate of bytes becomes a critical step. It is worth noting that the data sent is very fast, because this will lead to the single-chip computer there can not be processed in a timely manner, so the delay after each send is very important. Our microcontroller where the delay is 10 milliseconds, so we choose to send out each byte after the delay of 10 milliseconds and then send the next byte.
for (byte b : bytes) { out.write(b); Thread.sleep(10); }
6. For better physical examination to avoid excessive waste of traffic, using blocking InputStream read.
When using InputStream, it is important to note that InputStream reads are blocked. This does not affect our program in general, but it is important to remember that the code is designed, especially when considering the user experience. The
parameterless read () is very inefficient to read only one byte from the stream at a time, but simple, such as reading an integer value, using read () is very good, but what if it is a 16 binary string? Use Inputstream.read (byte[] b) or the Inputstream.read (byte[] b,int off,int len) method so that you can read multiple bytes at one time.
If you are reading multiple bytes, we often use the inputstream.available () method to get the number of readable bytes in the data stream. This method works very well when reading local data, but if you are reading non-local data, you may have a problem with byte misses, like reading 100 bytes, maybe 90, or even 0.
There are 0 cases where the microcontroller has no response or bytes have not been sent, then we need a loop to ensure that we can get the data:
int0; while0) { count = in.available(); } bytenewbyte[count]; in.read(bytes); 但像是上面的90个字节的情况就是字节遗漏。对于这种情况,解决方法也很简单: bytenewbyte[count]; int0// 已经成功读取的字节的个数 while (readCount < count) { readCount += in.read(bytes, readCount, count - readCount); }
Part of the Content reference link
The data transmission problem of Bluetooth