Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从可编程设备接收数据_Android_Bluetooth_Bluetooth Lowenergy - Fatal编程技术网

Android 从可编程设备接收数据

Android 从可编程设备接收数据,android,bluetooth,bluetooth-lowenergy,Android,Bluetooth,Bluetooth Lowenergy,我一直在为Android开发一款应用程序,它通过蓝牙与物理硬件进行通信。到目前为止,我们一直在使用普通蓝牙,但现在已改为BLE。我不是android开发者,为什么这对我来说有点困难 目前我正在进行概念验证。每当物理设备接收到某些内容时,它就会将其发送回,我可以使用适当的BLE终端应用程序确认这一点。我已经确定了需要使用的特性,可以连接到设备并向其发送数据。虽然我无法接收数据,但我从未收到任何回复 下面显示的代码显示了我当前正在执行的操作,尝试(成功)向设备发送数据,但未成功接收数据。我完全误解了

我一直在为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;

                        }
                    }
                }

            } 
        }

您的日志输出是什么?请注意,您只能有一个挂起的写入操作。您需要等待相应的回调,然后才能发送新的写入。您打算读取该特征还是接收关于该特征的通知?根据您的代码,我可以看到您正在尝试接收关于特定特征的通知。使用notify属性时,必须依靠对等设备发送通知。您能否交叉检查对等设备是否正确发送您案例中的通知?