我的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方法。我错过了什么?
发布于 2015-05-15 14:20:21
连接后,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);
}
}https://stackoverflow.com/questions/30260802
复制相似问题