Android Bluetooth 4.0 uses detailed code

Keywords: Android Mac Mobile socket

From https://www.jianshu.com/p/8bb7c11a2112


This time, I will explain the basic points of Bluetooth 4.0 and record it as my memo. First, popularize Bluetooth 4.0 based on Gatt protocol. Under Bluetooth 4.0, traditional Bluetooth is implemented in socket mode. So Bluetooth over 4.0 has the advantages of faster transmission speed, wider coverage, higher security, shorter delay, low consumption of electrodes and so on.

A BLE terminal can contain multiple services, a Service can contain multiple Characteristic, a Characteristic can contain one value and multiple Descriptors, and a Descriptor can contain one Value. Characteristic is very important. It is the key to exchange data between mobile phone and BLE terminal. Reading and setting data are all related attributes of Characteristic.
Next comes the code section:
1. The first is to declare authority:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

If you want to declare that your application can only run on BLE-enabled devices, you can include the following statement in your application manifest file:

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"></uses-feature>

2. Second, get Bluetooth adapter:
Bluetooth Adapter mBluetooth Adapter = Bluetooth Adapter. getDefaultAdapter (); also available

 BluetoothManager  mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
 BluetoothAdapter  mBluetoothAdapter = mBluetoothManager.getAdapter();

When mBluetooth Adapter==null, it means that the mobile phone does not have Bluetooth on. At this time, we can open Bluetooth by calling the Bluetooth window of the system, as follows

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);```
Re pass
 ``` @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
    }```
Turn back on success.
3. Next, look for Bluetooth. Finding Bluetooth is very simple. First of all, we define the callback interface of Bluetooth Finding and Acquisition Device, as follows:

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// device.getName(); Gets the Bluetooth device name
// device.getAddress(); Get the mac address of the Bluetooth device
}
} Then use mBluetooth Adapter. startLeScan (mLeScan Callback); start searching for devices and output device data information by calling back onLeScan (final Bluetooth Device device, int rssi, byte [] scanRecord) whenever there is a device. When you don't want to search again, you can use mBluetoothAdapter. stopLeScan (mLeScan Callback); ``to stop the search.

4. There is Bluetooth device information. Next, of course, we need to connect to Bluetooth, otherwise it will be useless to ask for this information. Connecting Bluetooth is also easy. It is suggested that Bluetooth connection should be done in the background service. If you save the BluetoothDevice object of the callback method above, you can run BluetoothGatt mBluetoothGatt= device.connectGatt(this, false, mGattCallback); this code connects, as described below. Bluetooth Gatt is also very important for this object. Later, it is found that services such as read and write devices are operated through this object. If there is no Bluetooth Device object and only the MAC address of the Bluetooth device can be connected, this can first obtain the Bluetooth Adapter Bluetooth Adapter object as above, Bluetooth Device = mBluetooth Adapter. getRemoteDevice (intent. getStringExtra ("mac"); then Bluetooth Device object can be connected by getRemoteDevice() method and then as above. So.
What is the mGattCallback object that appears in the connection code above? It is a Bluetooth connection, reading device, writing data to the device and notifying the device will call back the interface method, as follows:

private final BluetoothGattCallback mGattCallback=new BluetoothGattCallback() {
       
        //Callback this function when connected to a device or lost connection
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if(newState== BluetoothProfile.STATE_CONNECTED){
                Log.e("log_state","Successful connection");
                mBluetoothGatt.discoverServices();
            }else if(newState==BluetoothProfile.STATE_DISCONNECTED){
                Log.e("log_state","connection failed");
            }
            super.onConnectionStateChange(gatt, status, newState);
        }
        
        //When the device finds a service, it calls back the function
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {   //I found the service.
                //Here you can parse the service and find the service you need.
            } 
        }

         //The interface is invoked when the device notifies
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            Log.e("log_change","Sending notice");
        }

       //Callback this function when reading the device
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                //Read successful
                Log.e("log_read",characteristic.getValue()[0]+"");
            }else{
              //read failure
            }

        }
        
      //Callback this function when writing data to Characteristic
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            if(status == BluetoothGatt.GATT_SUCCESS){
              //Write successfully
            }else{
              //Write failure
            }
        }

      @Override //When data is written to the device Descriptor, the function is called back.
    public void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {
          super.onDescriptorRead(gatt, descriptor, status);
    }

    };

This is the callback method of the whole Bluetooth core, because all your Bluetooth operations are inseparable from this method. When we call the connectGatt() method to connect, we first go back to the onConnectionStateChange (Bluetooth Gatt gatt, int status, int newState); the method to see if the connection has been successful, the new state==Bluetooth Profile. STATE_CONNECTED; then we can use mBluetooth Gatt. Discovery Services (); method to find the service in the device. When the Bluetooth device service is searched, it calls back.
On Services Discovered (Bluetooth Gatt gatt, int status); method at this point you can traverse all services of Bluetooth devices, such as the following method:

private void displayGattServices(List<BluetoothGattService> gattServices) {
        if (gattServices == null)
            return;
        for (BluetoothGattService gattService : gattServices) {
           // Travel through all services in GATT services
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { 
            // Traveling through all Characteristic in each service 
               if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(Need to communicate UUID)) {
                    // What UUIDs are there, and what attributes and functions each UUID has, the general hardware engineer will give the corresponding documents. Our program can also read its attributes to determine its attributes.
                    // Here you can read, write, and set notification s to the device according to the type of UUID.
                    // Bluetooth GattCharacteristic gattNoticCharacteristic hypothesis is Characteristic with settable notifications
                    // Bluetooth GattCharacteristic gattWriteCharacteristic hypothesis is a readable Characteristic
                    // Bluetooth GattCharacteristic GattReadCharacteristic Hypothesis is Writable Characteristic
               }
            }
        }
    }

At this point, you need a hardware engineer to provide you with UUID documentation about the Bluetooth device. Every UUID function and operation needs documentation. Otherwise, you don't know what functions and how to use these UUIDs. When you know what UUID means, you can read and write through the Bluetooth GattCharacteristic class.

5. When you see from the document, the traversing UUID has the function of receiving and receiving notifications. Then you can set up an acceptable notification. The code is as follows:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
        if (descriptor != null) {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
}

setCharacteristicNotification() is invoked by getting the Bluetooth GattCharacteristic that corresponds to the notification UUID. Among them, 00002902-0000-1000-8000-00805f9b34fb is the UUID that the system provides to receive the notification. The notification is realized by setting Bluetooth GattDescriptor equivalent to setting the Descriptor attribute of Bluetooth GattCharacteristic, so as long as the Bluetooth device sends the notification signal, it will. Callback to the onCharacteristicChanged (Bluetooth Gatt, Bluetooth Gatt Characteristic characteristic) method, where you can do the corresponding logical processing.

6. Or when you traverse the UUID service about writing data to the device that has reached the control device UUID is, you can save the corresponding Bluetooth GattCharacteristic object. Then the data is written to the Bluetooth GattCharacteristic object, and the data is written to the Bluetooth GattCharacteristic object.
BluetoothGatt calls the writeCharacteristic() method to write data to the hardware, such as the following code:

sendCharacteristic.setValue(new byte[] {0x00});
mBluetoothGatt.writeCharacteristic(sendCharacteristic);

The data read out and written in general hardware is binary type, so you should be familiar with the conversion between integer, string, binary, hexadecimal and so on. As for what data to write, look at the hardware engineer's documentation.

7. Read as you write, and it's not difficult to read data from Bluetooth devices. First of all, we can find the UUID about reading Bluetooth device data from the traversing UUID, which UUID depends on the hardware document. Then save the corresponding Bluetooth GattCharacteristic object. When you want to read it, use Bluetooth Gatt's readCharacteristic (Bluetooth Gatt Characteristic characteristic) directly; the characteristic in the parameter is the Bluetooth Gatt Characteristic object you save, such as mBluetooth Gatt. readCharacteristic (getCharacteristic); and then call back the onCharacteristicRead (Bluetooth Gatt, Bluetooth Gatt Characteristic Characteristic) above. At last, when status = Bluetooth Gatt. GATT_SUCCESS, you can get the data returned by Bluetooth device through characteristic.getValue(); the data you get is your logical processing.

So far, the key knowledge of Bluetooth 4.0 has been written, you can connect Bluetooth devices to do a lot of things, of course, the premise is to have Bluetooth hardware documentation, otherwise you do not know what those UUID s mean and how to use them. If it's helpful to you, please give it to me. Thank you.



Author: Jack 921
Link: https://www.jianshu.com/p/8bb7c11a2112
Source: Brief Book
The copyright of the brief book belongs to the author. For any form of reprinting, please contact the author for authorization and indicate the source.

Posted by webbwbb on Tue, 14 May 2019 22:21:12 -0700