onServicesDiscovered没有被要求BluetoothGattCallbackonServicesDiscovered、BluetoothGattCallback

2023-09-07 00:15:54 作者:玍銹的英雄夢

我开始iBeacon显示和android的工作。但也有一些需要你的帮助的问题。在blutoothGatCallback我实现onConnectionStateChange并调用discoverServices()。 Althougth,discoverServices()返回true,但没有任何回调的执行,我希望onServicesDiscovered被称为,但不是。

I start working with ibeacon and android. But there are some problem need for your helps. On blutoothGatCallback I implement onConnectionStateChange and call discoverServices(). Althougth, discoverServices() return true but there is not any callback execute, I hope onServicesDiscovered being called, but not.

@Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
            int newState) {
        Log.i(TAG, "onConnectionStateChange : " + status + "  newState : "
                + newState);
        if (newState == BluetoothProfile.STATE_CONNECTED) {

            boolean isdiscover= mBluetoothGatt.discoverServices();
            if(isdiscover){
            mConnected = true;              
            }
        } 

    }

状态为133和newstate连接。难道地位使其轮不到回调

Status is 133 and newstate is connected. Does status make it fail to get callback

推荐答案

更​​多信息将是有益的,可以将所接收的广告数据?

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

下面仅仅是一个猜测与Android BLE左右那里有一个范围广泛的问题,但是我已经看到了这一点一般的133错误时不小心发出BR / EDR连接到信标。

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.

由于Android的默认为BR / EDR连接时,GATT服务器设备发布了对它的支持,你可以尝试显式地设置运输TRANSPORT_LE在connectGatt(),但由于该参数只在隐藏的版本aviable它,你需要使用反射。

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.

尝试发出这样的连接:

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();
        }
    }

这样,你至少可以肯定它不是与此有关Android的行为。

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

相关推荐