Android 未调用ble上的读取特征
我对ble非常陌生,仍然在尝试。在我的应用程序中,我正试图向ble设备发送一个字节数组,作为响应,我将从该设备接收一个字节数组。但是,不调用onCharacteristicRead。我知道我的代码中缺少了一些东西,但我无法找到它。请帮忙。先谢谢你。下面是我的代码:Android 未调用ble上的读取特征,android,android-ble,Android,Android Ble,我对ble非常陌生,仍然在尝试。在我的应用程序中,我正试图向ble设备发送一个字节数组,作为响应,我将从该设备接收一个字节数组。但是,不调用onCharacteristicRead。我知道我的代码中缺少了一些东西,但我无法找到它。请帮忙。先谢谢你。下面是我的代码: @Override public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Log.d("onServicesDiscovered", "Service uuid ");
List<BluetoothGattService> gattServices = gatt.getServices();
Log.d("onServicesDiscovered", "Services count: "+gattServices.size());
if (status == BluetoothGatt.GATT_SUCCESS) {
ArrayList<String> alst_uuid = new ArrayList<String>();
// List<BluetoothGattService> gattServices = gatt.getServices();
// Log.d("onServicesDiscovered", "Services count: "+gattServices.size());
for (BluetoothGattService gattService : gattServices) {
String serviceUUID = gattService.getUuid().toString();
if(serviceUUID.equalsIgnoreCase("56788877-e7cb-469b-6578-2742f1ba77cc"))
{
for(BluetoothGattCharacteristic characteristic: gattService.getCharacteristics())
{
if(characteristic.getUuid().toString().equals("xxxxxxx-b042-4876-aae1-112855353cc1"))
{
Log.d("foundoutchar", characteristic.getUuid().toString());
String originalString = "BB0100017E";
byte[] byt_arr = hexStringToByteArray(originalString);
characteristic.setValue(byt_arr); // call this BEFORE(!) you 'write' any stuff to the server
gatt.writeCharacteristic(characteristic);
}
}
}
alst_uuid.add(serviceUUID);
List<BluetoothGattCharacteristic> list = gattService.getCharacteristics();
Log.d("onServicesDiscovered", "Service uuid "+serviceUUID);
serviceObject object = new serviceObject();
String characteristics ="";
String properties ="";
for(int i = 0; i< list.size(); i++)
{
Log.d("onServicesDiscovered", "Service Characteristics "+list.get(i).getUuid());
characteristics = characteristics+""+list.get(i).getUuid()+"\\\n";
properties = properties+""+list.get(i).getProperties()+"\\\n";
Log.d("onServicesDiscovered", "Service Properties "+list.get(i).getProperties());
}
} else {
Log.d("log", "onServicesDiscovered received: " + status);
}
}
});
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
MainActivity.this.runOnUiThread(new Runnable() {
public void run()
{
Log.d("onCharacteristicWrite","writing"+characteristic.getUuid().toString());
}
});
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
MainActivity.this.runOnUiThread(new Runnable() {
public void run()
{
Log.d("onCharacteristic","writing"+characteristic.getUuid().toString());
}
});
}
};
BluetoothGatt bluetoothGatt = result.getDevice().connectGatt(MainActivity.this, true, gattCallback);
}
}
// auto scroll for text view
}
};
调用onCharacteristicRead是为了响应readCharacteristic命令,看起来代码并没有发出该命令 如果您的外围设备设置为自动用一个字节回复您写入的字节,那么您就好像在关注通知或指示。这些可以使用捕获,但您必须在设置期间为每个特征启用通知
如果您的设备未设计为使用通知进行回复,则您应该从onCharacteristicWrite回调发出readCharacteristic。BDW,我在onCharacteristicsWriteThank中的状态为0。请允许我检查。