2016-09-15 53 views
-1

奇怪的情况发生在相同的代码为Android 4.4.2操作系统三星和三星(A8)Android 5.1.1。Android Ble写

if(mBluetoothGattCharacteristic==null){ 
      return; 
     } 

     byte [] mCommandData=new byte[64]; 
     mCommandData[0]=Constants.REMOTE_CMD; 
     mCommandData[1]=(byte) mRemoteCommand; 

     mBluetoothGattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); 

     boolean isUpdated= mBluetoothGattCharacteristic.setValue(mCommandData); 
     boolean isWriteDone=mBluetoothGatt.writeCharacteristic(mBluetoothGattCharacteristic); 
     LogUtils.i("LOG", "Ble Write set is---"+ isUpdated + " Characteristics Write done"+ isWriteDone); 

     /** 
     * Create New Command Release Key 
     */ 

     byte [] mCommandRelease=new byte[64]; 

     mCommandRelease[0]=Constants.REMOTE_CMD; 
     mCommandData[1]=(byte)Constants.IR_KEY_RELEASE; 

     isUpdated= mBluetoothGattCharacteristic.setValue(mCommandData); 
     isWriteDone=mBluetoothGatt.writeCharacteristic(mBluetoothGattCharacteristic); 
     LogUtils.i("LOG", "Ble Write set is---"+ isUpdated + " Characteristics Write done"+ isWriteDone); 

在上面的代码为Android 4.4.2 writeCharacteristic两个命令写给真。

但是在android系统的情况5.1.1给它真正的第一次写,当它继续旁边写它提供虚假的writeCharacteristic

现在的解决方案我想到的是Android 5.1.1我不得不等待为onCharacteristicWrite,然后写下一个命令。

如果有人知道这样的问题,并指出我是否仅发生在三星设备上?

回答

-1

现在我通过把我的下一个命令onCharacteristicWrite理清问题,

我发现,我们不应该这样写代码:

BluetoothGattCharacteristic.setValue(mCommandData); 
boolean isWriteDone=mBluetoothGatt.writeCharacteristic(mBluetoothGattCharacteristic); 

然后再发送下一个命令:

BluetoothGattCharacteristic.setValue(mNextCommdn); 
boolean isWriteDone=mBluetoothGatt.writeCharacteristic(mBluetoothGattCharacteristic); 

因此,我们试图对GATT配置文件执行两次写操作,然后只有第一个可以成功执行。

所以我写第一个命令的结果接收我的下一个命令的基础: 这将在onCharacteristicWrite接收。

+1

这是正确的解决方案。在Android的BLE API中,一次只能有一次优秀的GATT操作。 – Emil