1

我正在尝试在 Sony SmartWatch 3 上实现 BluetoothGattServer。

我可以成功地打开 Gatt 服务器以及做广告。

使用BLE Scanner 应用程序,在发现我的自定义服务及其自定义特征时,我得到了不一致的结果。

  1. 在三星 Galaxy Note 8 - Android 4.4 - 我可以正确检测到自定义服务和自定义特征;
  2. 在 Nexus 7 - Android 5.0.1 - 我可以检测到我的自定义服务,但只有一个带有奇怪 UUID 的特征:00000000-0000-1000-8000-00805f9b34fb
  3. 在三星 Galaxy Note 8 上 - CynaogenMod Marshmallow - 与 Nexus 7 相同。

这是我在 Wear 设备上运行的代码:

public final class BluetoothServer extends BluetoothGattServerCallback{
    private final static String TAG = BluetoothServer.class.getSimpleName();
    private final static String BASE_UUID = "0000%s-0000-1000-8000-00805f9b34fb";


    public final static UUID MAIN_SERVICE = UUID.fromString(String.format(BASE_UUID, "FEE0"));
    public final static UUID CHARAC_REQUEST = UUID.fromString(String.format(BASE_UUID, "FEE01"));
    public final static UUID CHARAC_RECORDING = UUID.fromString(String.format(BASE_UUID, "FEE02"));


    private final BluetoothGattServer gattServer;

    private final BluetoothGattService mainServer;
    private final BluetoothGattCharacteristic requestCharacteristic;
    private final BluetoothGattCharacteristic recordingCharacteristic;

    public BluetoothServer(@NonNull final Context context) {
        final BluetoothManager btManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        final BluetoothAdapter btAdapter = btManager.getAdapter();

        this.gattServer = btManager.openGattServer(context.getApplicationContext(), this);

        this.mainService= new BluetoothGattService(MAIN_SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY);


    this.requestCharacteristic = new BluetoothGattCharacteristic(CHARAC_REQUEST, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);
    this.recordingCharacteristic = new BluetoothGattCharacteristic(CHARAC_RECORDING, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);

        this.mainService.addCharacteristic(this.requestCharacteristic);
        this.mainService.addCharacteristic(this.recordingCharacteristic);

        this.gattServer.addService(this.mainService);

        final BluetoothLeAdvertiser advertiser = btAdapter.getBluetoothLeAdvertiser();
        btAdapter.setName("Test Wear");
        final AdvertiseSettings settings = new AdvertiseSettings.Builder()
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
                .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
                .setConnectable(false)
                .build();

        final ParcelUuid pUuid = new ParcelUuid(MAIN_SERVICE);
        final AdvertiseData data = new AdvertiseData.Builder()
                .setIncludeDeviceName(true)
                .addServiceUuid(pUuid)
                .build();

        final AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
            @Override
            public final void onStartSuccess(final AdvertiseSettings settingsInEffect) {
                super.onStartSuccess(settingsInEffect);
                Log.i(TAG, "Started advertisement with success");
            }

            @Override
            public final void onStartFailure(final int errorCode) {
                Log.e(TAG, "Advertising onStartFailure: " + errorCode );
                super.onStartFailure(errorCode);
            }
        };

        advertiser.startAdvertising(settings, data, advertisingCallback );
    }

    //[... the rest of the callbacks implementation ...]
}

我还尝试使用来自https://www.uuidgenerator.net/的自定义生成的 UUID,但这样做更糟糕:要么我不能在包含名称的情况下做广告,要么如果我不包含名称,那么 BLE Scanner 应用程序将无法拾取我在所有设备上的定制服务。

我究竟做错了什么 ?

4

1 回答 1

2

你检查 onStartFailure() 方法了吗?

还有一件事,尽量不要在 AdvertiseData 中添加 UUID,在这里添加代码..!

public BluetoothServer(@NonNull final Context context) {
    final BluetoothManager btManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    final BluetoothAdapter btAdapter = btManager.getAdapter();

   this.mainService= new BluetoothGattService(MAIN_SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY);


this.requestCharacteristic = new BluetoothGattCharacteristic(CHARAC_REQUEST, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);
this.recordingCharacteristic = new BluetoothGattCharacteristic(CHARAC_RECORDING, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);

    this.mainService.addCharacteristic(this.requestCharacteristic);
    this.mainService.addCharacteristic(this.recordingCharacteristic);

    this.gattServer.addService(this.mainService);

    final BluetoothLeAdvertiser advertiser = btAdapter.getBluetoothLeAdvertiser();
    btAdapter.setName("Test Wear");
    final AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
            .setConnectable(true) //Set it true
            .build();

    final ParcelUuid pUuid = new ParcelUuid(MAIN_SERVICE);
    final AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
    //Remove this line
            .addServiceUuid(pUuid)
            .build();

    final AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
        @Override
        public final void onStartSuccess(final AdvertiseSettings settingsInEffect) {
            super.onStartSuccess(settingsInEffect);
            Log.i(TAG, "Started advertisement with success");
        }

        @Override
        public final void onStartFailure(final int errorCode) {
            Log.e(TAG, "Advertising onStartFailure: " + errorCode );
            super.onStartFailure(errorCode);
        }
    };

   //Line 1 Changed
   this.gattServer = btManager.openGattServer(context.getApplicationContext(), this);

    advertiser.startAdvertising(settings, data, advertisingCallback );
}
于 2017-07-31T10:12:21.953 回答