Android Use BluetoothManager#getConnectionState instead



我正在为AppCelerator-Titanium编写一个Android BLE模块,并且在与蓝牙设备的succesfull连接后,我使用的是蓝牙gatt对象。但是我想检查连接的当前状态。

我尝试使用:

public BluetoothGatt gatt;
private BluetoothDevice device;
@Kroll.method
public void discoverServices(){
    Log.i("PeripheralProxy", "discoverServices");
    if(gatt!=null){
        Log.i("PeripheralProxy", "Gatt is true discoverServices");
        try {
            if(device!=null){
                int connectionState= gatt.getConnectionState(device);
                Log.i("PeripheralProxy", "Device is not null");
            } else {
                Log.e("PeripheralProxy", "Device is null");
            }
        } catch (Exception e){
            Log.e("PeripheralProxy",e.getMessage());
        }
        //DISCOVER SERVICES
        boolean discoverResult = gatt.discoverServices();
        Log.i("PeripheralProxy", Boolean.toString(discoverResult));
    } else {
        Log.e("PeripheralProxy", "Gatt is null on discoverServices");
    }
}

但是,控制台返回以下例外:[错误]外围甲状腺素:( krollruntimethread([1,282875]使用bluetoothmanager#getConnectionState。

但是,在这种情况下,我无法访问蓝牙管理器。我如何从蓝牙gotem对象获取连接状态?

在Android文档上,他们具有此方法bluetoothgatt.getConnectionState(BluetoothDevice设备(访问:https://developer.android.com/reference/android/bluetooth/bluetoothgatt.html#getConnectionState(android.bluetooth.bluetooth.bluetoothdevice(

,但他们还说:不支持 - 请使用getConnectedDevices(int(用Gatt作为参数。但是,此函数再次要求在此上下文中我无法访问的蓝牙管理器。访问:https://developer.android.com/reference/android/bluetooth/bluetoothmanager.html#getConnectedDevices(int(

感谢您对此的帮助。

为什么不能使用

ble_manager=(BluetoothManager)getSystemService(BLUETOOTH_SERVICE);

然后您可以使用getConnectionState((方法。该对象应始终可用,因为Bluettoth_service是上下文的一部分。

最新更新