是一个训练项目,蓝牙模块使用的是MLT-BT05,采用的是CC2541蓝牙模块,模块带有背板,使用蓝牙模块的TTL接口与TTL转USB接口相连接。USB接口与PC连接。
手机型号:华为P20。手机安装官网提供的测试APP,PC使用串口调试助手。
开发环境:android studio3.4
手机端运行APP,可以扫描到蓝牙模块,可以与蓝牙模块建立连接,使用手机向蓝牙发送数据,在串口调试助手上可以接收到发送的数据。
问题在于:当使用串口调试助手向手机发送数据时,手机却接收不到数据。跟踪行动情况发现其中的“onCharacteristicRead”、“onCharacteristicChanged”方法均没有回调,有资料需要添加如下代码:
BluetoothGattDescriptor dsc =ale.getDescriptor(UUID.fromString( "00002902-0000-1000-8000-00805f9b34fb"));
byte[]bytes = {0x01,0x00};
dsc.setValue(bytes);
boolean success =mBluetoothGatt.writeDescriptor(dsc);但是该代码原代码是存在的。通过Wireshark抓包发现,串口调试器通过USB接口将数据正常发送了。
主要代码如下:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
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) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status) {
Log.i("DEBUG","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {
Log.i("DEBUG","BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
};
public void enable_JDY_ble( ){
try {
BluetoothGattService service =mBluetoothGatt.getService(UUID.fromString(Service_uuid));
BluetoothGattCharacteristic ale=service.getCharacteristic(UUID.fromString(Characteristic_uuid_TX));
boolean set = mBluetoothGatt.setCharacteristicNotification(ale, true);
Log.d(TAG," setnotification ============================================= " + set);
BluetoothGattDescriptor dsc =ale.getDescriptor(UUID.fromString( "00002902-0000-1000-8000-00805f9b34fb"));
byte[]bytes = {0x01,0x00};
dsc.setValue(bytes);
boolean success =mBluetoothGatt.writeDescriptor(dsc);
Log.d(TAG, "writing enabledescriptor:" + success);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}其中的两条日志信息输出的set 、success值均为true,但手机端收不到数据,Log.i("DEBUG","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");Log.i("DEBUG","BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");日志没有输出。
连接蓝牙了吗?