Android BLE低功耗蓝牙开发极简系列(二)之读写操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/muziby/article/details/78470456

这是Ble极简系列的第二篇文章,上一篇Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接主要是扫描连接,这一篇主要是读写操作。

发现服务

在连接成功后,可以通过Gatt进行discoverServices()。

     if (newState == BluetoothProfile.STATE_CONNECTED) {//当蓝牙设备已经连接
  //获取ble设备上面的服务
          Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
            Log.i("haha", "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());
            Log.d("haha", "onConnectionStateChange: " + "连接成功")
        }

在mGattCallback 回调添加Servicest的相关回调

  //发现服务回调。
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        Log.d("haha", "onServicesDiscovered: " + "发现服务 : " + status);
        if (status == BluetoothGatt.GATT_SUCCESS) {
                 //成功
            }
        }

读写开关

当返回的status == BluetoothGatt.GATT_SUCCESS时,进行读写以及通知相关的操作, 调用writeDescriptor(),注意设置setValue为ENABLE_INDICATION_VALUE,否则可能后续读取不到数据。

        if (status == BluetoothGatt.GATT_SUCCESS) {
                 //成功
             isServiceConnected = true;

            boolean serviceFound;
            Log.d("haha", "onServicesDiscovered: " + "发现服务 : " + status)

            if (mBluetoothGatt != null && isServiceConnected) {

                BluetoothGattService gattService = mBluetoothGatt.getService(UUID_SERVICE);
                BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID_NOTIFICATION);
                boolean b = mBluetoothGatt.setCharacteristicNotification(characteristic, true);
                if (b) {

                    List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
                    for (BluetoothGattDescriptor descriptor : descriptors) {

                        boolean b1 = descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                        if (b1) {
                            mBluetoothGatt.writeDescriptor(descriptor);
                            Log.d(TAG, "startRead: " + "监听收数据");
                        }

                    }

                }
            }

设置成功,会在onDescriptorWrite方法进行回调,注意UUID_SERVICE,UUID_NOTIFICATION特征值UUID,可以询问公司固件端的开发人员,和开发人员配合修改。

     @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorWrite(gatt, descriptor, status);
        Log.d(TAG, "onDescriptorWrite: " + "设置成功");
    }

发送数据

 public void startSend(View view) {
    if (mBluetoothGatt != null && isServiceConnected) {
        BluetoothGattService gattService = mBluetoothGatt.getService(UUID_SERVICE);
        BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID_WRITE);
        byte[] bytes = new byte[2];
        bytes[0] = 04;
        bytes[1] = 01;
        characteristic.setValue(bytes);
        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

}

读取数据

读取数据在onCharacteristicChanged方法中,注意进制间的转换。

    @Override
    public final void onCharacteristicChanged(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
        byte[] value = characteristic.getValue();
        Log.d(TAG, "onCharacteristicChanged: " + value);
        String s0 = Integer.toHexString(value[0] & 0xFF);
        String s = Integer.toHexString(value[1] & 0xFF);
        Log.d(TAG, "onCharacteristicChanged: " + s0 + "、" + s);
        for (byte b : value) {
            Log.d(TAG, "onCharacteristicChanged: " + b);
        }

    }

断开操作

  if (mBluetoothGatt != null) {

        mBluetoothGatt.close();
    }

注意事项

一定要进行读写开关操作,注意descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE),否则可能读取不到数据。

源码

BleDemo

猜你喜欢

转载自blog.csdn.net/muziby/article/details/78470456