本文介绍了Android 4.3的蓝牙BLE写操作执行后不叫onCharacteristicRead()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想执行读写操作与蓝牙peripheral.I在其中给予特定输入后,给我的输出,但里面code onCharacteristicRead不会被调用后onCharacteristicWrite写示例应用程序。请帮助我。
这是我的code
BluetoothGattCharacteristic WriteCharacteristic = NULL;
BluetoothGattCharacteristic ReadCharacteristic = NULL;
私人BluetoothGattCallback mGattCallback =新BluetoothGattCallback(){ / *状态机跟踪* /
私人INT mState = 0; 私人无效复位(){mState = 0; } 私人无效提前(){
mState ++;
} 公共无效onReliableWriteCompleted(GATT BluetoothGatt,诠释状态){
Log.e(onReliableWriteCompleted,被称为);
};
@覆盖
公共无效onConnectionStateChange(GATT BluetoothGatt,诠释状态,诠释newState){
Log.d(TAG,连接状态更改:+状态+ - >中+ connectionState(newState));
如果(状态== BluetoothGatt.GATT_SUCCESS&放大器;&安培; newState == BluetoothProfile.STATE_CONNECTED){
/ *
*一旦连接成功,就必须在下一个发现所有的服务
*设备之前,我们可以读取和写入各自的特点。
* /
gatt.discoverServices();
//字符串SERVICENAME = gatt.getService(BATTERY_DATA_CHAR)的ToString();
//System.out.println(servicename);
mHandler.sendMessage(Message.obtain(NULL,MSG_PROGRESS,发现服务...));
}否则如果(状态== BluetoothGatt.GATT_SUCCESS&放大器;&安培; newState == BluetoothProfile.STATE_DISCONNECTED){
/ *
*如果在我们断开任何时候,发送邮件来清除天气值
*总分的UI
* /
connectedDevice = NULL;
mHandler.sendEmptyMessage(MSG_CLEAR);
}否则如果(状态!= BluetoothGatt.GATT_SUCCESS){
/ *
*如果有故障的任何阶段,只需断开
* /
gatt.disconnect();
connectedDevice = NULL;
}
}
@覆盖
公共无效onServicesDiscovered(GATT BluetoothGatt,诠释状态){
Log.d(TAG,服务发现:+状态);
mHandler.sendMessage(Message.obtain(NULL,MSG_PROGRESS启用传感器...));
/ *
*使用服务发现,我们将重新设置我们的国家机器,并启动
*通过我们需要使传感器工作
* /
//复位();
字符串测试=;
清单< BluetoothGattService>则strName = gatt.getServices();
字符串SERVICENAME = DINSTANCE_SERVICE.toString();
对于(诠释计数= 0; COUNT< strname.size();计数++)
{
如果(servicename.equalsIgnoreCase(strname.get(计数).getUuid()。的toString()))
{ ReadCharacteristic = gatt.getService(DINSTANCE_SERVICE).getCharacteristic(DISTAINCE_READ_DATA);
WriteCharacteristic = gatt.getService(DINSTANCE_SERVICE).getCharacteristic(DISTANCE_WRITE_DATA);
测试=123;
//characteristic.getStringValue(18);
//字节BBB [] = characteristic.getValue();
// gatt.readCharacteristic(特性);
/ *字符串strInput =012072;
字节passvalues [] = strInput.getBytes();
WriteCharacteristic.setValue(passvalues);
gatt.writeCharacteristic(WriteCharacteristic); * / WriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
//tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
WriteCharacteristic.setValue(012072,BluetoothGattCharacteristic.FORMAT_SINT32,0);
gatt.writeCharacteristic(WriteCharacteristic);
打破; } }
的System.out.println(服务名称);
} 公共无效onDescriptorRead(GATT BluetoothGatt,BluetoothGattDescriptor描述,诠释状态)
{
Log.e(onDescriptorRead,Read方法被调用。);
}; @覆盖
公共无效onCharacteristicRead(GATT BluetoothGatt,BluetoothGattCharacteristic特征,诠释状态){
//对于每个读取,数据传递到UI线程更新显示
字节BBB [] = characteristic.getValue();
如果(HUMIDITY_DATA_CHAR.equals(characteristic.getUuid())){
mHandler.sendMessage(Message.obtain(NULL,MSG_HUMIDITY,特性));
}
如果(preSSURE_DATA_CHAR.equals(characteristic.getUuid())){
mHandler.sendMessage(Message.obtain(NULL,MSG_ preSSURE,特性));
}
如果(preSSURE_CAL_CHAR.equals(characteristic.getUuid())){
mHandler.sendMessage(Message.obtain(NULL,MSG_ preSSURE_CAL,特性));
}
如果(DISTAINCE_READ_DATA.equals(characteristic.getUuid()))
{
mHandler.sendMessage(Message.obtain(NULL,MSG_BATTERY,特性));
}
如果(BATTERY_READ_CHAR.equals(characteristic.getUuid()))
{
mHandler.sendMessage(Message.obtain(NULL,MSG_BATTERY,特性));
} //读取初始值后,接下来我们启用通知
// setNotifyNextSensor(GATT);
} @覆盖
公共无效onCharacteristicWrite(GATT BluetoothGatt,BluetoothGattCharacteristic特征,诠释状态)
{
//后写使能标志,接下来我们读到的初始值
// readNextSensor(GATT);
尝试{
如果(状态!= BluetoothGatt.GATT_SUCCESS)
抛出新的AssertionError(关于字符写入错误);
super.onCharacteristicWrite(GATT,特征,地位);
如果(characteristic.getUuid()。等于(DISTANCE_WRITE_DATA)){
BluetoothGattService syncService = gatt.getService(DINSTANCE_SERVICE);
BluetoothGattCharacteristic TCHAR = syncService.getCharacteristic(DISTANCE_WRITE_DATA);
ReadCharacteristic = syncService.getCharacteristic(DISTAINCE_READ_DATA);
如果(TCHAR == NULL)(「时同步时间特征空!)抛出新的AssertionError;
tChar.setValue(012072,BluetoothGattCharacteristic.FORMAT_SINT32,0);
gatt.readCharacteristic(ReadCharacteristic);
// gatt.readDescriptor(NULL);
}
}赶上(AssertionError的E){
e.printStackTrace();
}
} @覆盖
公共无效onCharacteristicChanged(GATT BluetoothGatt,BluetoothGattCharacteristic特性){
/ *
*后启用通知,从特征设备的所有更新
*价值的变动将被张贴在这里。类似的阅读,我们掌握了这些达
* UI线程更新显示。
* /
如果(HUMIDITY_DATA_CHAR.equals(characteristic.getUuid())){
mHandler.sendMessage(Message.obtain(NULL,MSG_HUMIDITY,特性));
}
如果(preSSURE_DATA_CHAR.equals(characteristic.getUuid())){
mHandler.sendMessage(Message.obtain(NULL,MSG_ preSSURE,特性));
}
如果(preSSURE_CAL_CHAR.equals(characteristic.getUuid())){
mHandler.sendMessage(Message.obtain(NULL,MSG_ preSSURE_CAL,特性));
}
} @覆盖
公共无效onDescriptorWrite(GATT BluetoothGatt,BluetoothGattDescriptor描述,诠释状态){
//一旦启用通知,我们移动到下一个传感器,并能重新开始
字符串strInput =012072;
字节passvalues [] = strInput.getBytes();
descriptor.setValue(passvalues);
提前();
Log.e(onDescriptorWrite,称为);
// enableNextSensor(GATT);
} @覆盖
公共无效onReadRemoteRssi(GATT BluetoothGatt,诠释RSSI,诠释状态)
{
Log.e(onReadRemoteRssi,称为);
Log.d(TAG,远程RSSI:+ RSSI);
} 私人字符串connectionState(INT状态){
开关(状态){
案例BluetoothProfile.STATE_CONNECTED:
返回已连接;
案例BluetoothProfile.STATE_DISCONNECTED:
回到断开连接;
案例BluetoothProfile.STATE_CONNECTING:
返回正在连接;
案例BluetoothProfile.STATE_DISCONNECTING:
回到断开
默认:
返回将String.valueOf(状态);
}
}
};
解决方案
在这里,您可以通过调用设置(订阅)通过任何通知或指示charactersitic:
mBluetoothGatt.setCharacteristicNotification(特性,真)
mBluetoothGatt.readCharacteristic(特性);
从这里
I want to perform read and write operation with bluetooth peripheral.I have write sample application in which after giving particular input it give me output but inside code onCharacteristicRead not get called after onCharacteristicWrite. Please help me out.
This is my code
BluetoothGattCharacteristic WriteCharacteristic =null;
BluetoothGattCharacteristic ReadCharacteristic =null;
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
/* State Machine Tracking */
private int mState = 0;
private void reset() { mState = 0; }
private void advance() {
mState++;
}
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
Log.e("onReliableWriteCompleted", "Called");
};
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.d(TAG, "Connection State Change: "+status+" -> "+connectionState(newState));
if (status == BluetoothGatt.GATT_SUCCESS && newState==BluetoothProfile.STATE_CONNECTED) {
/*
* Once successfully connected, we must next discover all the services on the
* device before we can read and write their characteristics.
*/
gatt.discoverServices();
//String servicename = gatt.getService(BATTERY_DATA_CHAR).toString();
//System.out.println(servicename);
mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Discovering Services..."));
} else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
/*
* If at any point we disconnect, send a message to clear the weather values
* out of the UI
*/
connectedDevice = null;
mHandler.sendEmptyMessage(MSG_CLEAR);
} else if (status != BluetoothGatt.GATT_SUCCESS) {
/*
* If there is a failure at any stage, simply disconnect
*/
gatt.disconnect();
connectedDevice = null;
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG, "Services Discovered: "+status);
mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Enabling Sensors..."));
/*
* With services discovered, we are going to reset our state machine and start
* working through the sensors we need to enable
*/
//reset();
String test="";
List<BluetoothGattService> strname = gatt.getServices();
String servicename =DINSTANCE_SERVICE.toString();
for(int count=0;count<strname.size();count++)
{
if(servicename.equalsIgnoreCase(strname.get(count).getUuid().toString()))
{
ReadCharacteristic = gatt.getService(DINSTANCE_SERVICE).getCharacteristic(DISTAINCE_READ_DATA);
WriteCharacteristic = gatt.getService(DINSTANCE_SERVICE).getCharacteristic(DISTANCE_WRITE_DATA);
test="123";
//characteristic.getStringValue(18);
//byte bbb[] = characteristic.getValue();
// gatt.readCharacteristic(characteristic);
/*String strInput = "012072";
byte passvalues[] = strInput.getBytes();
WriteCharacteristic.setValue(passvalues);
gatt.writeCharacteristic(WriteCharacteristic);*/
WriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
//tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
WriteCharacteristic.setValue(012072, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
gatt.writeCharacteristic(WriteCharacteristic);
break;
}
}
System.out.println(servicename);
}
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
{
Log.e("onDescriptorRead","Read method gets called.");
};
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//For each read, pass the data up to the UI thread to update the display
byte bbb[] = characteristic.getValue();
if (HUMIDITY_DATA_CHAR.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_HUMIDITY, characteristic));
}
if (PRESSURE_DATA_CHAR.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_PRESSURE, characteristic));
}
if (PRESSURE_CAL_CHAR.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_PRESSURE_CAL, characteristic));
}
if(DISTAINCE_READ_DATA.equals(characteristic.getUuid()))
{
mHandler.sendMessage(Message.obtain(null, MSG_BATTERY, characteristic));
}
if(BATTERY_READ_CHAR.equals(characteristic.getUuid()))
{
mHandler.sendMessage(Message.obtain(null, MSG_BATTERY, characteristic));
}
//After reading the initial value, next we enable notifications
//setNotifyNextSensor(gatt);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
{
//After writing the enable flag, next we read the initial value
// readNextSensor(gatt);
try {
if (status != BluetoothGatt.GATT_SUCCESS)
throw new AssertionError("Error on char write");
super.onCharacteristicWrite(gatt, characteristic, status);
if (characteristic.getUuid().equals(DISTANCE_WRITE_DATA)) {
BluetoothGattService syncService = gatt.getService(DINSTANCE_SERVICE);
BluetoothGattCharacteristic tChar = syncService.getCharacteristic(DISTANCE_WRITE_DATA);
ReadCharacteristic = syncService.getCharacteristic(DISTAINCE_READ_DATA);
if (tChar == null)throw new AssertionError("characteristic null when sync time!");
tChar.setValue(012072,BluetoothGattCharacteristic.FORMAT_SINT32, 0);
gatt.readCharacteristic(ReadCharacteristic);
// gatt.readDescriptor(null);
}
} catch (AssertionError e) {
e.printStackTrace();
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
/*
* After notifications are enabled, all updates from the device on characteristic
* value changes will be posted here. Similar to read, we hand these up to the
* UI thread to update the display.
*/
if (HUMIDITY_DATA_CHAR.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_HUMIDITY, characteristic));
}
if (PRESSURE_DATA_CHAR.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_PRESSURE, characteristic));
}
if (PRESSURE_CAL_CHAR.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_PRESSURE_CAL, characteristic));
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
//Once notifications are enabled, we move to the next sensor and start over with enable
String strInput = "012072";
byte passvalues[] = strInput.getBytes();
descriptor.setValue(passvalues);
advance();
Log.e("onDescriptorWrite", "called");
//enableNextSensor(gatt);
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status)
{
Log.e("onReadRemoteRssi", "called");
Log.d(TAG, "Remote RSSI: "+rssi);
}
private String connectionState(int status) {
switch (status) {
case BluetoothProfile.STATE_CONNECTED:
return "Connected";
case BluetoothProfile.STATE_DISCONNECTED:
return "Disconnected";
case BluetoothProfile.STATE_CONNECTING:
return "Connecting";
case BluetoothProfile.STATE_DISCONNECTING:
return "Disconnecting";
default:
return String.valueOf(status);
}
}
};
解决方案
Here you can set up (subscribe) to the charactersitic through either notification or indication by calling:
mBluetoothGatt.setCharacteristicNotification(characteristic, true)
mBluetoothGatt.readCharacteristic(characteristic);
from here
这篇关于Android 4.3的蓝牙BLE写操作执行后不叫onCharacteristicRead()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!