Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger. https://blog.csdn.net/u010046908/article/details/50610297
How to use Android Bluetooth 2.0
1.Android operation Bluetooth 2.0 use process
(1) find the device uuid(2) obtain the Bluetooth adapter, make Bluetooth in the discoverable mode, acquire the socket of the lower computer, and establish a connection with the upper computer,
Obtain the input stream and output stream. If neither stream is empty, the connection is successful. Otherwise, the connection fails.
(3) start communication with the socket of the lower computer.
(4) after communication, disconnect (close flow and socket)
2 next, connect to the code directly:
2.1 find the UUID of the device (usually provided by the manufacturer to the developer)
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
2.2 establish connection with Bluetooth device
BluetoothAdapter myBluetoothAdapter = null;//Bluetooth adapter BluetoothServerSocket mBThServer = null;//Upper computer < span style = "font family: Arial, Helvetica, sans serif;" > ServerSocket</span> BluetoothSocket mBTHSocket = null;//socket of lower computer InputStream mmInStream = null;//Input stream OutputStream mmOutStream = null;//Output stream <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//Get adapter Set<BluetoothDevice> pairedDevices = myBluetoothAdapter .getBondedDevices();//Get all Bluetooth devices under the adapter if (pairedDevices.size() > 0) { for (Iterator<BluetoothDevice> iterator = pairedDevices .iterator(); iterator.hasNext();) { BluetoothDevice device = (BluetoothDevice) iterator .next(); if (DEVICE_NAME1.equals(device.getName()) || DEVICE_NAME2.equals(device.getName()) || DEVICE_NAME3.equals(device.getName()) || DEVICE_NAME4.equals(device.getName())) { try { myBluetoothAdapter.enable();//Make adapter settings available Intent discoverableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);// Make Bluetooth in discoverable mode for 150s discoverableIntent .putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 150); mBTHSocket = device .createRfcommSocketToServiceRecord(MY_UUID);//Get the socket of the lower computer int sdk = Integer.parseInt(Build.VERSION.SDK); if (sdk >= 10) { mBTHSocket = device .createInsecureRfcommSocketToServiceRecord(MY_UUID); } else { mBTHSocket = device .createRfcommSocketToServiceRecord(MY_UUID); } mBThServer = myBluetoothAdapter .listenUsingRfcommWithServiceRecord( "myServerSocket", MY_UUID);Monitor available devices mBTHSocket.connect(); // Establish connection mmInStream = mBTHSocket.getInputStream();// Get input stream mmOutStream = mBTHSocket.getOutputStream();// Get output stream } catch (IOException e) { ett.setText("Device connection is abnormal!"); } if ((mmInStream != null) && (mmInStream != null))// If they are not empty, the connection is successful, otherwise, the connection fails { ett.setText("Device connected successfully!"); } else { ett.setText("Device connection failed!"); } break; } } } }
2.3 start sending data and read data (byte array)
Note: both CMD find and recData are byte arrays byte []if ((mmInStream == null) || (mmInStream == null)) { Readflage = -2;// Connection exception return; } mmOutStream.write(cmd_find);//Write find instruction Thread.sleep(200); int datalen = mmInStream.read(recData);//Read data
The above code is the step of sending instructions and reading data at one time. Very simple.
2.4 disconnection
if ((mmInStream == null) || (mmInStream == null)) { return; } //Turn off streams and socket s mmOutStream.close(); mmInStream.close(); mBTHSocket.close(); mBThServer.close();
Finally, there are three basic steps, the first is to establish a connection, the second is to send data to read data, and the third is to disconnect. That's all for today. I will write about the use of Bluetooth 4.0 ble in Android later. There are still many differences between the two. Please look forward to it