在Android中通过BLE发送数据必须使用哪些服务和特性?

时间:2016-07-05 18:40:00

标签: android bluetooth-lowenergy android-ble

我开发了一个Android应用程序来向BLE设备发送数据。当我连接到BLE设备时,我发现了服务和特性并得到了这个

onGetService() - Device=D8:80:39:F0:03:6E UUID=00001800-0000-1000-8000-00805f9b34fb
onGetService() - Device=D8:80:39:F0:03:6E UUID=0000180a-0000-1000-8000-00805f9b34fb
onGetService() - Device=D8:80:39:F0:03:6E UUID=49535343-fe7d-4ae5-8fa9-9fafd205e455
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a00-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a01-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a04-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a29-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a24-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a25-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a27-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a26-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a28-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a23-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=00002a2a-0000-1000-8000-00805f9b34fb
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=49535343-6daa-4d02-abf6-19569aca69fe
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=49535343-aca3-481c-91ec-d85e28a60318
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=49535343-1e4d-4bd9-ba61-23c647249616
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=49535343-8841-43f4-a8d4-ecbe34729bb3
onGetCharacteristic() - Device=D8:80:39:F0:03:6E UUID=49535343-026e-3a9b-954c-97daef17e26e
onGetDescriptor() - Device=D8:80:39:F0:03:6E UUID=00002902-0000-1000-8000-00805f9b34fb
onGetDescriptor() - Device=D8:80:39:F0:03:6E UUID=00002902-0000-1000-8000-00805f9b34fb
onGetDescriptor() - Device=D8:80:39:F0:03:6E UUID=00002902-0000-1000-8000-00805f9b34fb
onSearchComplete() = Device=D8:80:39:F0:03:6E Status=0

所以我认为我的设备有3个服务和16个特性(每个服务都有不同的特性),问题是当我必须发送一个字符时,我使用这个代码

 public void writeCustomCharacteristic(int value) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    /*check if the service is available on the device*/
    BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("00001800-0000-1000-8000-00805f9b34fb"));
    if(mCustomService == null){
        Log.w(TAG, "Custom BLE Service not found");
        return;
    }
    /*get the read characteristic from the service*/
    BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("00002a04-0000-1000-8000-00805f9b34fb"));
    mWriteCharacteristic.setValue(value,android.bluetooth.BluetoothGattCharacteristic.FORMAT_UINT8,0);
    if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false){
        Log.w(TAG, "Failed to write characteristic");
    }
}

所以问题是,必须使用哪些服务和特性?我发现了我发现的东西。我想向BLE设备发送一个简单的字符,并从UART类型的接口中恢复。

1 个答案:

答案 0 :(得分:0)

我认为您应该使用该服务:

  

49535343-fe7d-4ae5-8fa9-9fafd205e455

用于与特征进行数据交换:

  

49535343-8841-43f4-a8d4-ecbe34729bb3

用于写入数据