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

时间:2017-04-27 13:35:20

标签: android bluetooth bluetooth-lowenergy android-ble bluetooth-peripheral

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

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

使用BLE Scanner application,在发现自定义服务及其自定义特征时,结果不一致。

  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棉花糖 - 与Nexus 7相同。
  4. 以下是我在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应用程序赢得了#39;在所有设备上启动我的自定义服务。

    我做错了什么?

1 个答案:

答案 0 :(得分:0)

您是否检查过onStartFailure()方法?

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

return (num >= (1 << shl)) * shl;