Java android ble无法调用onCharacteristicRead()

Java android ble无法调用onCharacteristicRead(),java,android,bluetooth,bluetooth-lowenergy,Java,Android,Bluetooth,Bluetooth Lowenergy,我目前正在开发一个android应用程序,目的是从腕带获取数据,并对其进行处理,以获得类似健康评估的东西。我被困在这个问题上,无法让onCharacteristicRead()函数工作。。。它只是不会被呼叫 以下是与本部分相关的代码: private BluetoothGattCallback gattcallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(Blu

我目前正在开发一个android应用程序,目的是从腕带获取数据,并对其进行处理,以获得类似健康评估的东西。我被困在这个问题上,无法让onCharacteristicRead()函数工作。。。它只是不会被呼叫

以下是与本部分相关的代码:

private BluetoothGattCallback gattcallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, final int newState) {
        super.onConnectionStateChange(gatt, status, newState);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String status;
                switch (newState) {
                    case BluetoothGatt.STATE_CONNECTED:
                        lianjiezhuangtai.setText("connection succeed");
                        bluetoothAdapter.stopLeScan(callback);
                        bluetoothGatt.discoverServices();
                        break;
                    case BluetoothGatt.STATE_CONNECTING:
                        lianjiezhuangtai.setText("trying to connect");
                        break;
                    case BluetoothGatt.STATE_DISCONNECTED:
                        lianjiezhuangtai.setText("connection lost");
                        break;
                    case BluetoothGatt.STATE_DISCONNECTING:
                        lianjiezhuangtai.setText("disconnecting");
                        break;
                }
                //pd.dismiss();
            }
        });
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        jibu.setText("run: trying to get steps");
        super.onCharacteristicRead(gatt, characteristic, status);

        if (status == bluetoothGatt.GATT_SUCCESS) {
            final int sum = 100;//characteristic.getValue()[0];

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    jibu.setText("walked" + sum + "steps");
                }
            });

            Log.e(TAG, "onCharacteristicRead: " + characteristic.getValue()[0]);

        }

    }        
    @Override
    public void onServicesDiscovered(final BluetoothGatt gatt,final int status) {
        super.onServicesDiscovered(gatt, status);
        Log.i(TAG, "run: trying to get steps");
        if (status == bluetoothGatt.GATT_SUCCESS) {
            final List<BluetoothGattService> services = bluetoothGatt.getServices();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //List<String> serlist = new ArrayList<>();
                    for (final BluetoothGattService bluetoothGattService : services) {
                        bluetoothGattServices = bluetoothGattService;

                        Log.i(TAG, "onServicesDiscovered: " + bluetoothGattService.getUuid());

                        List<BluetoothGattCharacteristic> charc = bluetoothGattService.getCharacteristics();

                        for (BluetoothGattCharacteristic charac : charc) {
                            Log.i(TAG, "run: " + charac.getUuid());
                            // 00002a06-0000-1000-8000-00805f9b34fb 
                    //        bluetoothGatt.setCharacteristicNotification(characteristic_zd,true);
                            if (charac.getUuid().toString().equals("0000ff06-0000-1000-8000-00805f9b34fb")) {
                                characteristic_zd = charac;

                            } else if (charac.getUuid().toString().equals("00002a06-0000-1000-8000-00805f9b34fb")) {

                                characteristic_jb = charac;

                               // bluetoothGatt.setCharacteristicNotification(characteristic_jb,true);
                                bluetoothGatt.readCharacteristic(characteristic_jb);
                                //sum = charac.getValue()[0];
                                gattcallback.onCharacteristicRead(gatt, characteristic_jb, status);
                                Log.i(TAG, "run: trying to get steps");
                            } else if (charac.getUuid().toString().equals("")) {
                            }
                        }


                        serviceslist.add(bluetoothGattService.getUuid().toString());

                    }
//                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
//                                MainActivity.this, android.R.layout.simple_expandable_list_item_1, serviceslist);
                    //list.setAdapter(adapter);
                }
            });
        }

    }



    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
    }


    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);



    }

};
private BluetoothGattCallback gattcallback=new BluetoothGattCallback(){
@凌驾
连接状态更改的公共无效(蓝牙gatt、国际贸易总协定、国际贸易状态、最终国际贸易新闻状态){
超级连接状态更改(关贸总协定、状态、新状态);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
字符串状态;
交换机(新闻状态){
案例蓝牙gatt.STATE\u已连接:
连杰庄台.setText(“连接成功”);
bluetoothAdapter.stopLeScan(回调);
蓝牙GATT.discoverServices();
打破
案例蓝牙gatt.STATE\u连接:
“试图连接”);
打破
案例蓝牙gatt.STATE\u已断开:
LIANJIECHAUGTAI.SETEXT(“连接丢失”);
打破
案例蓝牙gatt.STATE\u断开连接:
连杰庄台.setText(“断开”);
打破
}
//pd.解散();
}
});
}
@凌驾
特征读取的公共无效(蓝牙gatt、蓝牙gatt特征特征、国际状态){
setext(“run:tryingtogetsteps”);
超级特征阅读(关贸总协定,特征,地位);
如果(状态==蓝牙GATT.GATT\U成功){
final int sum=100;//characteristic.getValue()[0];
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
jibu.setText(“走”+和+“步”);
}
});
Log.e(标记“onCharacteristicRead:+characteristic.getValue()[0]);
}
}        
@凌驾
发现服务上的公共无效(最终蓝牙gatt、最终int状态){
超级服务发现(关贸总协定,状态);
i(标记“run:tryingtogetsteps”);
如果(状态==蓝牙GATT.GATT\U成功){
final List services=bluetoothGatt.getServices();
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
//List serlist=new ArrayList();
用于(最终蓝牙服务蓝牙服务:服务){
bluetoothGattServices=bluetoothGattService;
Log.i(标记“OnServicesDiscovery:+bluetoothGattService.getUuid());
List charc=bluetoothGattService.getCharacteristics();
for(蓝牙gatt特征字符:charc){
Log.i(标记“run:+charac.getUuid());
//00002a06-0000-1000-8000-00805f9b34fb
//bluetoothGatt.setCharacteristicNotification(特征_zd,真);
如果(charac.getUuid().toString()等于(“0000ff06-0000-1000-8000-00805f9b34fb”)){
特性_zd=charac;
}else if(charac.getUuid().toString()等于(“00002a06-0000-1000-8000-00805f9b34fb”)){
特性_jb=charac;
//bluetoothGatt.setCharacteristicNotification(特征,正确);
蓝牙GATT.readCharacteristic(characteristic_jb);
//sum=charac.getValue()[0];
gattcallback.onCharacteristicRead(gatt,特性,状态);
i(标记“run:tryingtogetsteps”);
}else if(charac.getUuid().toString()等于(“”){
}
}
添加(bluetoothGattService.getUuid().toString());
}
//ArrayAdapter适配器=新的ArrayAdapter(
//MainActivity.this,android.R.layout.simple\u可扩展\u列表\u项目\u 1,服务列表);
//list.setAdapter(适配器);
}
});
}
}
@凌驾
特征写入的公共无效(蓝牙gatt、蓝牙gatt特征特征、int状态){
超级特征写入(关贸总协定、特征、状态);
}
@凌驾
特征变更后的公共无效(蓝牙gatt,最终蓝牙gatt特征特征){
超级特征改变(关贸总协定,特征);
}
};
有人能帮我解决这个问题吗?寻找解决方案已经有一段时间了,但仍然无法解决这个问题。。。非常感谢

三件事:

  • 您不需要在每个回调中调用super.method()
  • 您不应该自己调用onCharacteristicRead,在调用readCharacteristic方法后检索到读取响应后,蓝牙堆栈将执行此操作
  • 如果有不止一个特性,您的代码将无法工作,因为Android的GATTAPI一次只能有一个未完成的操作。这意味着您需要等待onCharacteristicRead回调,然后才能执行另一个readCharacteristic。(如果您打印readCharacteristic的返回值,您将看到它第一次打印为true,但随后打印为false。)

  • 我在试图阅读关贸总协定的多项特征时遇到了同样的问题。呼叫readCha