一光年lost 2016-08-11 03:30 采纳率: 57.1%
浏览 2665
已采纳

ble不能回调onCharacteristichanged方法

public void readManufacturer(){
    BluetoothGattService manufacturerService = mBluetoothGatt.getService(UUID.fromString(MANUFACTURER_SERVICE));
    if (manufacturerService != null) {
        //Log.d("demo", String.valueOf(manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC))));
        BluetoothGattCharacteristic manufacturerCharacteristic = manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC));
        Log.d("demo", String.valueOf(manufacturerCharacteristic));
        Log.d("demo", String.valueOf(manufacturerCharacteristic != null));
        if (manufacturerCharacteristic != null) {
            Log.d("demo","intoNotify");
            setCharacteristicNotification(manufacturerCharacteristic,true);
        }
    }
}


     public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,boolean enable){
    Log.d("demo", "setCharacteristicNotification");
    if(mBluetoothAdapter == null || mBluetoothGatt == null){
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enable);
    if(UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())){
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
}

    打出来的Log:
    08-11 14:58:54.594 4768-4768/com.example.chongyanghu.mybletest D/demo: readBatteryAction

08-11 14:58:54.595 4768-4768/com.example.chongyanghu.mybletest D/demo: android.bluetooth.BluetoothGattCharacteristic@1893971
08-11 14:58:54.595 4768-4768/com.example.chongyanghu.mybletest D/demo: true
08-11 14:58:54.595 4768-4768/com.example.chongyanghu.mybletest D/demo: intoNotify
08-11 14:58:54.595 4768-4768/com.example.chongyanghu.mybletest D/demo: setCharacteristicNotification

进入了setCharacteristicNotification,为什么没有回调onCharacteristicChanged方法,求大牛帮忙看看~

  • 写回答

2条回答 默认 最新

  • 一光年lost 2016-08-11 07:31
    关注

    自己解决了
    public void readManufacturer() {
    BluetoothGattService manufacturerService = mBluetoothGatt.getService(UUID.fromString(MANUFACTURER_SERVICE));
    if (manufacturerService != null) {
    //Log.d("demo", String.valueOf(manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC))));
    BluetoothGattCharacteristic manufacturerCharacteristic = manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC));
    Log.d("demo", "manufacturerCharacteristic = " + String.valueOf(manufacturerCharacteristic != null));
    final int charaProp = manufacturerCharacteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
    Log.d("demo", "intoNotify--->" + String.valueOf((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0));
    readCharacteristic(manufacturerCharacteristic);
    }
    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波