1

我的应用程序之一是作为蓝牙外围设备工作,我在其中添加了一个自定义服务和 3 个特征。我的客户端应用程序连接到外围设备后发现它的服务,但我的服务的 UUID 在不同的客户端中是不同的。我的一台三星设备完美地返回了我在宣传我的服务时添加的 UUID,而另一台设备显示了不同的 UUID。现在,如果服务 UUID 不匹配,我正在检查服务内部的特征,但令人惊讶的是,所有特征都在该服务内部。但是服务 UUID 不是我在服务中添加的。

这是我发现服务的代码

if(newState == BluetoothProfile.STATE_CONNECTED) {
                isConnected =true;
                Log.i(BluetoothClientActivity.class.getSimpleName(),
                        "Connected to GATT server.");
                // Attempts to discover services after successful connection.
                Log.i(BluetoothClientActivity.class.getSimpleName(),
                        "Attempting to start service discovery:");
                gatt.discoverServices();

            }

添加具有三个特征的自定义服务的示例代码

UUID PRIMARY_SERVICE = UUID.fromString("145d6dba-7064-11ea-bc55-0242ac130003");
    mSampleService = new BluetoothGattService(PRIMARY_SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY);

    mSampleService.addCharacteristic(characteristic1);
    mSampleService.addCharacteristic(characteristic2);
    mSampleService.addCharacteristic(characteristic3);

有什么方法可以识别我的服务和内部特征,可用于服务器和客户端之间的通信。

List<BluetoothGattService> services = gatt.getServices();
for(Service service:services){
      if(service.getUuid().toString().equals(SERVICE_UUID_STRING)){
         Log.e(TAG,"Found");
     }else{
        Log.e(TAG,"Not Found");
     }
}
4

0 回答 0