首页 新闻 会员 周边 捐助

android手机蓝牙APP开发,但手机无法接收蓝牙数据

0
[待解决问题]

是一个训练项目,蓝牙模块使用的是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");日志没有输出。

calmmoon的主页 calmmoon | 菜鸟二级 | 园豆:206
提问于:2019-06-12 15:11
< >
分享
所有回答(1)
0

连接蓝牙了吗?

沉迷编程的程序员 | 园豆:219 (菜鸟二级) | 2019-07-03 15:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册