onServicesDiscovered not be called on BluetoothGattCallback

╄→尐↘猪︶ㄣ 提交于 2019-11-29 12:55:28

More information would be helpful, can you supply the received Advertisement data?

The following is just a guess as theres a broad range of issues around with Android BLE, however i seen this general 133 error already when accidentally issuing a BR/EDR connection to a Beacon.

As Android defaults to a BR/EDR connection when the GATT-server device advertises support for it, you can try to explicitely set the transport to TRANSPORT_LE in connectGatt(), however since this parameter is only aviable in the hidden version of it, you need to use reflection.

Try to issue the connection like this:

private BluetoothGatt mGatt;
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { ... }

public void connectToDevice(BluetoothDevice device) {
        try {
            Method m = device.getClass().getDeclaredMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class);
            int transport = device.getClass().getDeclaredField("TRANSPORT_LE").getInt(null);
            mGatt = (BluetoothGatt) m.invoke(device, this, false, gattCallback, transport);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

That way you can at least be sure its not related to this Android behaviour.

Status 133 can indicate that you try to execute bt commands on a non-main thread.

The BTLE callbacks are called from android-bt-binder threads.

So

mBluetoothGatt.discoverServices();

runs on the binder thread and it should run on the GUI/main thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!