我的device.connectGatt()只触发onConnectionStateChage。状态为0,并建立连接。我在4.4和5.1系统上进行了测试 - 结果相同。这是我的代码:
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方法。我错过了什么?
答案 0 :(得分:8)
连接后,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);
}
}