1

我正在尝试连接到 BlueGiga BLE113 设备和我的三星 Galaxy S4(Android 4.3)。我可以成功发现设备但无法连接和发现服务。这是按下按钮连接后的日志。

12-30 16:38:34.012: D/BluetoothGatt(11280): registerApp()
12-30 16:38:34.012: D/BluetoothGatt(11280): registerApp() - UUID=5a5ac8ad-7583-457f-ba60-373c3beaf1b2
12-30 16:38:34.022: D/BluetoothGatt(11280): onClientRegistered() - status=0 clientIf=8
12-30 16:38:34.022: I/BluetoothGatt(11280): Client registered, waiting for callback
12-30 16:38:34.022: D/BluetoothGatt(11280): onClientConnectionState() - status=0 clientIf=8 device=FF:FF:FF:FF:FF:FF

传递给 connectGatt 方法的回调如下。

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            Log.i(TAG, "Trying to connect...");
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.i(TAG, "Connected to GATT server.");   
                gatt.discoverServices();

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                Log.i(TAG, "Disconnected from GATT server.");
            }   
        }
};
4

1 回答 1

2

这很可能是线程问题。我在三星 Galaxy S4 设备上遇到了与 BLE 非常相似的问题。看来三星的 Android 实现处理 BLE 的方式与其他设备不同(Nexus 7 设备运行良好)。尽管如此,您必须从 UI 线程显式运行您的 BLE connectGatt 方法。这是一个例子:

// Create handler for main thread where mContext is application context
mHandler = new Handler(mContext.getMainLooper());
...
// Connect to BLE device from mHandler
mHandler.post(new Runnable() {
    @Override
    public void run() {
        mBTGatt = mBTDevice.connectGatt(mContext, false, mGattCallback);
    }
});
于 2014-05-05T17:58:22.433 回答