繁体   English   中英

Android BLE外围设备模式:未检测到自定义特征

[英]Android BLE Peripheral Mode: custom characteristics not detected

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

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

使用BLE扫描仪应用程序时 ,发现自定义服务及其自定义特征时,结果不一致。

  1. 在Samsung Galaxy Note 8-Android 4.4上,我可以正确检测到自定义服务和自定义特征;
  2. 在Nexus 7-Android 5.0.1上-我可以检测到我的自定义服务,但只能检测到具有奇怪UUID的特征: 00000000-0000-1000-8000-00805f9b34fb
  3. 在Samsung Galaxy Note 8-CynaogenMod棉花糖-与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扫描仪应用程序将无法启动我在所有设备上的自定义服务。

我究竟做错了什么 ?

您是否检查过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 );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM