Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.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 将gatt特性写入BLE设备时出现问题_Android_Bluetooth Lowenergy_Gatt - Fatal编程技术网

Android 将gatt特性写入BLE设备时出现问题

Android 将gatt特性写入BLE设备时出现问题,android,bluetooth-lowenergy,gatt,Android,Bluetooth Lowenergy,Gatt,我正在尝试使用将自定义特征写入可编程设备 在DeviceControlActivity上,当我在获取的BluetoothGattService中循环时,我尝试为其中一个设置一个特性。在这一点上,我只是随机挑选一个进行测试 private void displayGattServices(List<BluetoothGattService> gattServices) { ... // Loops through available GATT Services.

我正在尝试使用将自定义特征写入可编程设备

DeviceControlActivity
上,当我在获取的
BluetoothGattService
中循环时,我尝试为其中一个设置一个特性。在这一点上,我只是随机挑选一个进行测试

private void displayGattServices(List<BluetoothGattService> gattServices) {
    ...

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        uuid = gattService.getUuid().toString();
        if(uuid.equals("00001800-0000-1000-8000-00805f9b34fb") && !firstTime){
            firstTime = true;
            BluetoothGattCharacteristic customChar = new BluetoothGattCharacteristic(MY_CHARACTERISTIC,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
            BluetoothGattCharacteristic.PERMISSION_READ |BluetoothGattCharacteristic.PERMISSION_WRITE);

            byte[] val = new byte[20];
            val[0] = 71;
            val[1] = 97;
            val[2] = 108;
            val[3] = 97;
            val[4] = 120;
            val[5] = 121;
            val[6] = 32;
            val[7] = 70;
            val[8] = 105;
            val[9] = 116;
            val[10] = -30;
            val[11] = -109;
            val[12] = -108;
            val[13] = 32;
            val[14] = 40;
            val[15] = 70;
            val[16] = 57;
            val[17] = 66;
            val[18] = 57;
            val[19] = 41;
            customChar.setValue(val);
            boolean isAdded = gattService.addCharacteristic(customChar);
            Log.d(TAG,"CARAC ADDED? "+isAdded);
        }

    ...
}
我还试图建立一个
GattServer
,并创建一个具有新特性的新服务,但也没有成功。以下是代码(此部分使用Kotlin):


如果您想作为GATT客户机写入,您应该在服务中查找特征,设置一个值,然后调用writeCharacteristic。“new”和“add”调用只能在您想要构建GATT服务器时使用。@Emil我尝试了您的建议,但都不起作用。我用代码片段更新了这个问题
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            ...
            uuid = gattCharacteristic.getUuid().toString();
            // UUID for device name
            if(uuid.equals("00002a00-0000-1000-8000-00805f9b34fb") && !firstTime){
                firstTime = true;
                gattCharacteristic.setValue("Hello");
               boolean isAdded = mBluetoothLeService.writeCharacteristic(gattCharacteristic);
                Log.d(TAG,"CARAC ADDED? "+isAdded); // returns false
            }
mBluetoothGattServer = mBluetoothManager?.openGattServer(this@MainActivity,gattCallback)
        mBluetoothGattServer?.connect(result.device,false)

val gattCallback = object: BluetoothGattServerCallback() {
    override fun onConnectionStateChange(device: BluetoothDevice?, status: Int, newState: Int) {
        super.onConnectionStateChange(device, status, newState)
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            val service = BluetoothGattService(UUID.fromString("f000aa01-0451-4000-b000-000000000000"), BluetoothGattService.SERVICE_TYPE_PRIMARY)
            val characteristic = BluetoothGattCharacteristic(UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb"), 
                    BluetoothGattCharacteristic.PROPERTY_READ or BluetoothGattCharacteristic.PROPERTY_WRITE or BluetoothGattCharacteristic.PROPERTY_NOTIFY, 
                    BluetoothGattCharacteristic.PERMISSION_READ or BluetoothGattCharacteristic.PERMISSION_WRITE)
            characteristic.addDescriptor(BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"), BluetoothGattCharacteristic.PERMISSION_WRITE))
            service.addCharacteristic(characteristic)
            mBluetoothGattServer?.addService(service)
         }
    }
    // Not sure if this is needed but it never triggers.
    override fun onCharacteristicWriteRequest(device: BluetoothDevice?, requestId: Int, characteristic: BluetoothGattCharacteristic?, preparedWrite: Boolean, responseNeeded: Boolean, offset: Int, value: ByteArray?) {
        super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value)
        mBluetoothGattServer?.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, "Hello".toByteArray())
    }