我目前正在开发一个项目,开发一个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);
}
};
那么有人可以帮我解决这个问题吗?一直在寻找解决方案但仍然无法解决这个问题...非常感谢
答案 0 :(得分:0)
三件事: