简体   繁体   English

BLE android 读取读取特性

[英]BLE android read readCharacteristic

I Try to devlope android app to read from cycling sensor,我尝试开发 android 应用程序来读取自行车传感器,
if anyone can help plz.如果有人可以帮助PLZ。 its connect to the sensor but cant read the characteristic.它连接到传感器但无法读取特性。 i dont know how to read the characterestic value.我不知道如何读取特征值。 its a speed sensor sor i want to be notify or read (i dont know the exact its read or notify)它是一个速度传感器,所以我想被通知或读取(我不知道它的确切读取或通知)

///////////////////////////LOGCAT//////////////////////////// //////////////////////////LOGCAT/////////////////////// //////

 onScanResult() - ScanResult{mDevice=F7:88:0B:34:04:5F
onScanResult: F7:88:0B:34:04:5F:35007-2 
connect() - device: F7:88:0B:34:04:5F, auto: false
registerApp()
registerApp() - UUID=b71dffd9-b87e-4ac9-9985-28ad9934745b
onClientRegistered() - status=0 clientIf=6
onClientConnectionState() - status=0 clientIf=6 device=F7:88:0B:34:04:5F
discoverServices() - device: F7:88:0B:34:04:5
 onSearchComplete() = Device=F7:88:0B:34:04:5F Status=0

//////////////////////LIST ALL SERVISES/////////////////////
setCharacteristicNotification() - uuid: 00002a5b-0000-1000-8000-00805f9b34fb enable: true

this is the code;这是代码;

    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
                  @Override
                  public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                      super.onConnectionStateChange(gatt, status, newState);
        
                      if (newState == BluetoothProfile.STATE_CONNECTED) {
                          gatt.discoverServices();
                      } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                          ScanningEnd = false;
                      }
                  }
        
                  @Override
                  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                      super.onServicesDiscovered(gatt, status);
        
                      List<BluetoothGattService> services = gatt.getServices();
                      Log.e("onServicesDiscovered", "Services count: "+services.size());
        
                      for (BluetoothGattService service : gatt.getServices()) {
                          Log.d(TAG, "Found Service " + service.getUuid().toString());
                          for(BluetoothGattCharacteristic mcharacteristic :service.getCharacteristics())
                          {
                              Log.d(TAG, "Found Characteristic " + mcharacteristic.getUuid().toString());}}
   
                     characteristicNotifi = gatt.getService(NOTIF_SERVICE)
                              .getCharacteristic(NOTIF_CHARACTERESTIC);
        
                      gatt.setCharacteristicNotification(characteristicNotifi, true);
                      gatt.readCharacteristic(characteristicNotifi);
        
                  }

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                      super.onCharacteristicChanged(gatt, characteristic);              
                      if (characteristic.equals(characteristicNotifi)) {
        
                          runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
                                  Toast.makeText(MainActivity.this, "value changed", Toast.LENGTH_LONG);
}});}}};
        
        
              @Override
              public void onScanFailed(int errorCode) {
                  super.onScanFailed(errorCode);
              }
        
          };
    
        }

.

See this answer for how to subscribe to notifications correctly using Android.请参阅此答案,了解如何使用 Android 正确订阅通知。

You need to do two steps to activate notifications on both the Android phone and the remote BLE device:您需要执行两个步骤来激活 Android 电话和远程 BLE 设备上的通知:

  1. Enable notification handling on your Android phone:在您的 Android 电话上启用通知处理:
gatt.setCharacteristicNotification(characteristicNotifi, true);
  1. Enable notifications on your BLE device for every characteristic you want:在 BLE 设备上为您想要的每个特征启用通知:
// 0x2902 org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor = characteristicNotifi.getDescriptor(uuid);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);

These steps are also described in the Android BLE Guide , but the second step is a little bit obscured, unfortunately.这些步骤在Android BLE Guide中也有描述,但不幸的是第二步有点晦涩难懂。

This should be enough to get a callback on your onCharacteristicChanged .这应该足以在您的onCharacteristicChanged上获得回调。

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

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