简体   繁体   English

Java BLE不返回任何服务

[英]Java BLE returning no services

I am creating an Android application that uses BLE to communicate to a MCU, currently I am able to make connection to the MCU using my application but when I attempt to discover and get services for the device I am returned a null array and am unsure as to why. 我正在创建一个使用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);
        }
    }

The output: 输出:

09-26 16:33:09.648 10917-10931/bamboomobile.medhead I/TAG: Connected to GATT server. 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() - device: DC:1B:A3:10:B9:D5 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: Attempting to start service discovery:true 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: Unhandled exception in callback java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at bamboomobile.medhead.MainMenu$1.onConnectionStateChange(MainMenu.java:86) at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:186) at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70) at android.os.Binder.execTransact(Binder.java:446) 09-26 16:33:09.668 10917-10931 / bamboomobile.medhead W / BluetoothGatt:回调java.lang.IndexOutOfBoundsException中未处理的异常:无效的索引0,java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255的大小为0 )的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 09-26 16:33:09.688 10917-10931 / bamboomobile.medhead D / BluetoothGatt:onSearchComplete()= Device = DC:1B:A3:10:B9:D5 Status = 0

Once connected, you need to call mGatt.discoverServices() . 连接后,您需要调用mGatt.discoverServices() Then in the onServicesDiscovered callback you can use mGatt.getServices() and proceed. 然后,可以在onServicesDiscovered回调中使用mGatt.getServices()并继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM