从BLE设备接收数据

时间:2019-12-22 12:44:32

标签: android bluetooth bluetooth-lowenergy

我一直在为Android开发一个应用程序,该应用程序通过蓝牙与物理硬件进行通信。到目前为止,我们一直在使用普通的蓝牙,但是现在已更改为BLE。我不是Android开发人员,为什么这对我来说有点困难。

目前,我只是在进行概念验证。每当物理设备接收到某种东西时,它就会将其发送回去,我可以使用适当的BLE终端应用程序进行确认。我已经确定了需要使用的特征,并且可以连接到该设备并向其发送数据。尽管我无法接收数据,但我再也收不到任何东西。

下面显示的代码显示了我当前正在做的事情,试图(成功)发送到设备,但没有成功接收数据。我完全误解了这个概念吗?

希望有人可以帮我

            @Override
        public void onConnectionStateChange(BluetoothGatt
                                                    gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            switch (newState) {
                case BluetoothProfile.STATE_CONNECTED:
                    Log.i("GattCallback", "connected");
                    gatt.discoverServices();
                    break;
                case BluetoothProfile.STATE_DISCONNECTED:
                    Log.i("GattCallback", "Disconnected");
                    break;
                default:
                    System.out.println(newState);
            }
        }



        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            byte[] newValue = characteristic.getValue();
            System.out.println("RECEIVED: " + Arrays.toString(newValue));
        }


        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {

                for (BluetoothGattService gattService : gatt.getServices()) {
                    Log.i("derp", "onServicesDiscovered: service=" + gattService.getUuid());
                    for (BluetoothGattCharacteristic characteristic : gattService.getCharacteristics()) {
                        Log.i("derp", "onServicesDiscovered: characteristic=" + characteristic.getUuid());


                        if(characteristic.getUuid().toString().contains("9616")){
                                System.out.println("We have found the read characteristic.");
                                gatt.setCharacteristicNotification(characteristic, true);
                                BluetoothGattDescriptor desc = characteristic.getDescriptor(UUID.fromString("49535343-1e4d-4bd9-ba61-23c647249616"));
                                if(desc == null){
                                    System.out.println("NULL!");
                                    return;
                                }
                                desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                                gatt.writeDescriptor(desc);
                                System.out.println("Should be subscribed.");


                        }
                        else if(characteristic.getUuid().toString().contains("9bb3")){
                            System.out.println("Send characteristic found");

                                    characteristic.setValue("Test\n\r");
                                    boolean res = gatt.writeCharacteristic(characteristic);
                                    if(res)
                                        System.out.println("Success");


                            break;

                        }
                    }
                }

            } 
        }

0 个答案:

没有答案