我想用蓝牙外设执行读写操作。我已经编写了一个示例应用程序,在给出特定输入后它给出了输出但内部代码onCharacteristicRead在onCharacteristicWrite之后没有被调用。请帮帮我。
这是我的代码
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);
}
}
};