简体   繁体   English

如何在Android中的BLE中执行/写入循环中的所有特征

[英]How to execute/write all characteristics in a loop in ble in android

How to write all writeCharacteristics in a loop in ble in android.code is as follows in this method i pass 3 writecharacteristic() and only one is write and others are ignored. 如何在android.code中的ble中的循环中编写所有writeCharacteristics,如下所示,在此方法中,我通过3 writecharacteristic() ,只有一个是write,其他的则被忽略。

if (beartoggle.isChecked()) {
    if (mDeviceLight.equalsIgnoreCase("on") && mDeviceAlarm.equalsIgnoreCase("on")) {
        byte[] val = {1};
        if (check_port_1 == 1) {
            mBluetoothLeService.writeCharacteristic(val, 1);    
        }

        if (check_port_2 == 1) {
            mBluetoothLeService.writeCharacteristic(val, 2);
        }
        if (find_me == 1) {
            mBluetoothLeService.writeCharacteristic(val, 3);
        }

    }
}

and someone is saying use public void onReliableWriteCompleted() and check if port1 is written then go to port2 and then findme. 有人说使用public void onReliableWriteCompleted()并检查是否写入了port1,然后转到port2,然后找到findme。 This method will help me, if yes then how ? 这种方法对我有帮助,如果可以,怎么办? Please send me clear details and following is my writeCharacteristic() 请给我清楚的详细信息,以下是我的writeCharacteristic()

 public boolean writeCharacteristic(byte value[], int type) {

     //check mBluetoothGatt is available
     if (mBluetoothGatt == null) {
         Log.e(TAG, "lost connection");

         return false;
     }
     BluetoothGattService Service = mBluetoothGatt.getService(UUID_SIMPLESERVICE);
     if (Service == null) {
         Log.e(TAG, "service not found!");
         return false;
     }
     BluetoothGattCharacteristic charac1 = null;
     BluetoothGattCharacteristic charac2 = null;
     BluetoothGattCharacteristic charac3 = null;
     boolean status1 = false, status2 = false, status3 = false;
     Log.v("___TYPE___", "________1______________" + (type == 1));
     Log.v("___TYPE___", "________2______________" + (type == 2));
     Log.v("___TYPE___", "________3______________" + (type == 3));
     onReliableWriteCompleted(status1);
     onReliableWriteCompleted(status2);
     onReliableWriteCompleted(status3);
     if (type == 1) {
         charac1 = Service.getCharacteristic(UUID_PORT1);
         charac1.setValue(value);
         status1 = mBluetoothGatt.writeCharacteristic(charac1);
         Log.v("________BLESERVICE____", "___WRITE CHARATERISTICS STATUS:_________" + status1);
         onReliableWriteCompleted(status1);
     } else if (type == 2) {
         charac2 = Service.getCharacteristic(UUID_PORT2);
         charac2.setValue(value);
         status2 = mBluetoothGatt.writeCharacteristic(charac2);
         onReliableWriteCompleted(status2);
         Log.v("________BLESERVICE_______", "___WRITE CHARACTERISTICS STATUS_______" + status2);

     } else if (type == 3) {
         charac3 = Service.getCharacteristic(UUID_FINDME);
         charac3.setValue(value);
         status3 = mBluetoothGatt.writeCharacteristic(charac3);
         onReliableWriteCompleted(status3);
         Log.v("__________BLESERVICE_________", "___WRITE CHARACTERISTICS STATUS_____" + status3);
     }

     if (charac1 == null && charac2 == null && charac3 == null) {
         Log.e(TAG, "char not found!");
         return false;
     }

     Log.v("___TYPE___", "______________________" + type);
     return status1 && status2 && status3;
 }

call BluetoothLeService.getSupportedGattServices() , you will get a list of services. 调用BluetoothLeService.getSupportedGattServices() ,您将获得服务列表。 Iterate through it and call BluetoothGattService.getCharacteristics() . 对其进行迭代,然后调用BluetoothGattService.getCharacteristics() You will again get a list, you can iterate through it. 您将再次获得一个列表,可以对其进行遍历。

For more info, you can refer this 有关更多信息,您可以参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM