繁体   English   中英

如何使用Android BLE API连接到GATT服务器?

[英]How to connect to a GATT server using Android BLE API?

我具有此功能来调用将我正在开发的应用程序连接到另一个启用BLE的设备:

public boolean connecttoDevice(int position) {
    currentDevice = devices.get(position);
    final BluetoothManager bluetoothManager = (BluetoothManager) _client
            .getSystemService(Context.BLUETOOTH_SERVICE);
    if(currentDevice != null 
       && bluetoothManager.getConnectionState(currentDevice, BluetoothProfile.GATT) != BluetoothProfile.STATE_CONNECTED 
       && bluetoothManager.getConnectionState(currentDevice, BluetoothProfile.GATT) != BluetoothProfile.STATE_CONNECTING
    ){
        mBluetoothGatt = currentDevice.connectGatt(_client, false, mGattCallback);
        System.out.println(currentDevice.getName()+":"+currentDevice.getAddress());
        System.out.println(bluetoothManager.getConnectionState(currentDevice, BluetoothProfile.GATT));
    }
    else return false;
    return !(mBluetoothGatt == null);
}

_client是调用此方法的活动。 我发现在调用该方法之后,设备的连接状态根本不会更改,并且永远不会调用回调。 我只是想知道哪里出错了?

谢谢,鲍勃

不用担心,目前API中存在一个错误,如果您要连接到BT经典和BLE双模设备,则连接将失败。

暂无
暂无

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

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