程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> android ble4.0 藍牙-ble不能回調onCharacteristichanged方法

android ble4.0 藍牙-ble不能回調onCharacteristichanged方法

編輯:編程解疑
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方法,求大牛幫忙看看~

最佳回答:


自己解決了
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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved