Android 4.3的蓝牙BLE不叫onCharacteristicRead() [英] android 4.3 Bluetooth ble don't called onCharacteristicRead()
问题描述
我已经设置通知到机器人,它不打电话来的方法 onCharacteristicRead()
????
它不进入到函数。为什么它是这样发生的事情??
I've set the notification into android, It is not calling to method onCharacteristicRead()
????
It does not enter into the function. Why it is happening so??
任何帮助是pciated AP $ P $
Any help is appreciated
请求的解决方案。
这是我的code:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:"
+ mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disconnected from GATT server.");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
gattServices = mBluetoothGatt
.getService(SampleGattAttributes.SERVICES_UUID);
if (gattServices != null) {
gattCharacteristics = gattServices
.getCharacteristic(SampleGattAttributes.CHARACTERISTIC_UUID);
System.out.println("character-->" + gattCharacteristics);
}
if (gattCharacteristics != null) {
System.out.println("Characteristic not null");
System.out.println("Characteristic Properties-->"
+ gattCharacteristics.getProperties());
mBluetoothGatt.setCharacteristicNotification(gattCharacteristics,
true);
}
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
System.out.println("in read");
if (status == BluetoothGatt.GATT_SUCCESS) {
byte[] data = characteristic.getValue();
System.out.println("reading");
System.out.println(new String(data));
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
//
System.out.println("change");
byte[] data = characteristic.getValue();
System.out.println(new String(data));
}
};
感谢你在前进!
推荐答案
首先 onCharacteristicRead
如果您已阅读特征将火:
First of all onCharacteristicRead
will fire if you have read a characteristic by:
mBluetoothGatt.readCharacteristic(characteristic);
阅读的特性和设置的通知是两回事。什么是你想从你的数据特性的类型?
Reading a characteristic and setting up notifications are two different things. What is the type of your characteristic you want to get data from?
它是:
- 阅读
- 通知
- 表示
如果是读
您可以使用 mBluetoothGatt.readCharacteristic(特性)读取的特性;
方法,但如果其通知
或显示
首先你要读的特点的描述
通过调用:
If it is read
you can read the characteristic using the mBluetoothGatt.readCharacteristic(characteristic);
method but if its notify
or indicate
first you will have to read the characteristic's descriptor
by calling:
mBluetoothGatt.readDescriptor(ccc);
在你读它,它应该通过调用 onDescriptorRead
回调返回数据。结果
在这里,您可以通过调用设置(订阅)通过任何通知或指示charactersitic:
Once you read it, it should return data by calling the onDescriptorRead
callback.
Here you can set up (subscribe) to the charactersitic through either notification or indication by calling:
mBluetoothGatt.setCharacteristicNotification(characteristic, true)
一旦返回真正
您将需要再次写入描述符(的通知或指示的值)
once it returns true
you will need to write to the descriptor again (the value of notification or indication)
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
// or
//clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(clientConfig);
一旦做到这一点,你会得到通知throuhg onCharacteristicChanged
回调每次特征性改变。
您可以阅读更多有关在这里
蓝牙连接>
大约蓝牙特性这里
you can read more about Bluetooth connection on Android here
and about Bluetooth Characteristics here
这篇关于Android 4.3的蓝牙BLE不叫onCharacteristicRead()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!