Android BluetoothGattCallback仅调用onConnectionStateChange
我的设备.connectGatt()仅触发onConnectionStateChage。状态为0,连接已建立。我在4.4和5.1系统上进行了测试,结果相同。这是我的代码:Android BluetoothGattCallback仅调用onConnectionStateChange,android,bluetooth-lowenergy,Android,Bluetooth Lowenergy,我的设备.connectGatt()仅触发onConnectionStateChage。状态为0,连接已建立。我在4.4和5.1系统上进行了测试,结果相同。这是我的代码: private final BluetoothGattCallback myCallBack = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status,
private final BluetoothGattCallback myCallBack = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
//BluetoothGattCharacteristic tempChar = null;
//tempChar.setValue("test");
if(status == 0) {
gatt.connect();
//gatt.disconnect();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
// Try to send some data to the device
characteristic.setValue("test");
gatt.writeCharacteristic(characteristic);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public boolean equals(Object o) {
return super.equals(o);
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public String toString() {
return super.toString();
}
};
我为所有调用添加了一个断点,唯一被击中的是onConnectionStateChange。这应该是这样的吗?我在哪里可以写出特征或描述并将其推送到BLE模块?此外,BLE模块(由Arduino控制的HC-10)正在发送数据(使用不同的应用程序进行测试)。。。所以我希望它也能击中onCharacteristicOnChage方法。我错过了什么 连接后,Android不知道设备上有任何服务或特征 因此,一旦连接,您必须
discoverServices()
:
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();
}
}
收到回调后,您可以执行写入操作:
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattCharacteristic writeChar = mBluetoothGatt.getService(myServiceUUID)
.getCharacteristic(myWriteCharUUID);
byte[] data = new byte[10];
writeChar.setValue(data);
gatt.writeCharacteristic(writeChar);
}
}
谢谢,实际上触发了OnServicesDiscovery,是用于将数据写入连接设备的onCharacteristicWrite,或者只有在实现外部“写入”时才会触发此方法onCharacteristicWrite是通过响应操作写入的响应现在它更有意义了,如果由于外部写入操作触发onCharacteristicWrite,那么应该在什么时候执行写入方法才能命中onCharacteristicWrite方法?在我心目中,这个装置;是连接、读取和写入应该发生的位置。您可以在
OnServicesDiscovery
回调中或之后的任何时间进行写入、设置通知等操作。我不确定我是否理解这将如何工作,您能给我举个例子吗?另外,我会继续接受你的回答,因为它回答了我最初的问题,但我仍然无法完成写作。如果您能提供一个简单的例子作为编辑,我将不胜感激。谢谢。如果不喜欢某个问题的人至少提到他们不喜欢它的原因,那就太棒了,这样我就可以改进它或者删除它