Java BLE 不返回任何服务
Posted
技术标签:
【中文标题】Java BLE 不返回任何服务【英文标题】:Java BLE returning no services 【发布时间】:2016-09-26 20:39:08 【问题描述】:我正在创建一个使用 BLE 与 MCU 通信的 android 应用程序,目前我可以使用我的应用程序连接到 MCU,但是当我尝试发现并获取设备的服务时,我返回了一个空数组和我不确定为什么。
//Code used to connect to the device
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:" + mGatt.discoverServices());
services = mGatt.getServices();
System.out.println("Service 1 is : " + services.get(0));
else if (newState == BluetoothProfile.STATE_DISCONNECTED)
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i("TAG", "Disconnected from GATT server.");
broadcastUpdate(intentAction);
输出:
09-26 16:33:09.648 10917-10931/bamboomobile.medhead I/TAG:已连接到 GATT 服务器。
09-26 16:33:09.648 10917-10931/bamboomobile.medhead D/BluetoothGatt:discoverServices() - 设备:DC:1B:A3:10:B9:D5
09-26 16:33:09.658 10917-10931/bamboomobile.medhead I/TAG:正在尝试启动服务发现:true
09-26 16:33:09.668 10917-10931/bamboomobile.medhead W/BluetoothGatt:回调中未处理的异常 java.lang.IndexOutOfBoundsException:索引 0 无效,大小为 0 在 java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 在 java.util.ArrayList.get(ArrayList.java:308) 在bamboomobile.medhead.MainMenu$1.onConnectionStateChange(MainMenu.java:86) 在 android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:186) 在 android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70) 在 android.os.Binder.execTransact(Binder.java:446)
09-26 16:33:09.688 10917-10931/bamboomobile.medhead D/BluetoothGatt: onSearchComplete() = Device=DC:1B:A3:10:B9:D5 Status=0
【问题讨论】:
【参考方案1】:连接后,您需要致电mGatt.discoverServices()
。然后在onServicesDiscovered
回调中,您可以使用mGatt.getServices()
并继续。
【讨论】:
以上是关于Java BLE 不返回任何服务的主要内容,如果未能解决你的问题,请参考以下文章
为啥 java.sql.ResultSet 的“getFloat(column)”方法不返回任何内容,而其他方法则返回?
Java - H2 数据库 - getGeneratedKeys() 不返回任何结果