0

我正在开发一个 android 应用程序,将树莓派连接到本地 wifi 网络,通过蓝牙从我的 android 手机向它们发送 ssid 和 ssid 密码信息。rpi 正在使用 bleno 运行节点服务器。

我的应用程序的设置如下。

  1. 我扫描显示设备清单供用户选择的 ble 设备
  2. 选择后,用户单击“下一步”按钮打开一个新的活动绑定到一个新的BluetoothLeService.
  3. 此活动有一个确认按钮,单击该按钮会启动 ble 外围设备与手机的连接过程。
  4. 将 ssid 和 ssid_pwd 数据发送到外围设备后,我会拆除并解除绑定BluetoothLeService,完成该活动,然后启动成功屏幕活动,将用户路由回扫描屏幕以重新开始该过程。

所以那部分一切都很好。我什至可以选择多个外围设备来发送两个数据,这很有效。

我的问题出现在第 4 步之后。如果用户在完成后决定选择另一个外围设备,onServiceConnected则会调用新外围设备,但mBluetoothGatt.discoverServices()似乎会选择两个服务。一次用于先前连接的外围设备,一次用于新的外围设备。

注意:这仅在我从服务中取消绑定然后稍后再次绑定时才会发生。绑定一次并使用两个外围设备似乎可行。

这可能是什么原因造成的?我已经检查了十多次BluetoothLeService被破坏的活动,绑定到的活动BluetoothLeService被破坏,甚至检查了第一个外围设备没有广播或接受 ble 连接。我什至已经从物理上拔掉了第一个 rpi。显示btsnoop_hci.log连接到一个外围设备,然后连接到另一个,因此它必须在应用程序代码中。有没有人有任何想法?

这是开始混乱的代码部分。

    private final BluetoothGattCallback mGattCallback =
        new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                                int newState) {
                String intentAction;
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    intentAction = ACTION_GATT_CONNECTED;
                    mConnectionState = STATE_CONNECTED;
                    broadcastUpdate(intentAction,gatt);
                    Log.i(TAG, "Connected to GATT server.");
                    Log.i(TAG, "Attempting to start service discovery:" +
                            mBluetoothGatt.discoverServices()); // <-- The problem starts here

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    intentAction = ACTION_GATT_DISCONNECTED;
                    mConnectionState = STATE_DISCONNECTED;
                    Log.i(TAG, "Disconnected from GATT server.");
                    broadcastUpdate(intentAction,gatt);
                }
            }

            @Override
            // New services discovered
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
                    Log.w(TAG, "onServicesDiscovered received: " + status);

                } else {
                    Log.e("ERROR", "Gatt onServiceDiscovered failed. Error code + " + status);
                }
            }
            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt,
                                              BluetoothGattCharacteristic characteristic,
                                              int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
                }
                Log.d("DEBUG", characteristic.getValue().toString());
            }
            @Override
            // Result of a characteristic read operation
            public void onCharacteristicRead(BluetoothGatt gatt,
                                             BluetoothGattCharacteristic characteristic,
                                             int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
                }
                Log.d("DEBUG", characteristic.getValue().toString());
            }
            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt,
                                                BluetoothGattCharacteristic characteristic) {
                Log.d("DEBUG", characteristic.getValue().toString());
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }


        };

谢谢

4

1 回答 1

0

当一个人注册一个接收器时,一个人也必须取消注册。

编辑:对于任何进入 android ble 的人来说,Github 上的googlesamples/android-BluetoothLeGatt存储库是生命线。

https://github.com/googlesamples/android-BluetoothLeGatt/tree/master/Application/src/main/java/com/example/android/bluetoothlegatt

于 2018-04-29T21:18:48.130 回答