0
我目前正在開發一款Android應用程序,旨在從腕帶獲取數據並處理它們以獲取健康評估內容。而我被困在這個問題上,我不能讓onCharacteristicRead()函數來工作...它只是不會得到所謂的...android ble無法調用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);
}
};
所以任何人都可以幫助我解決這個問題嗎?一直在尋找了一段時間的解決方案,仍然解決不了這個......非常感謝