首页 > 解决方案 > 在线程中通过 BLE 发送数据

问题描述

目前,我正在使用线程将数据从我的应用程序发送到北欧委员会。只有在我从北欧委员会(通知)获得答复后才能再次发送。你可以看到我的代码已经可以工作了,但是性能不是很好。我正在使用 AtomicBoolean 来确定我是否已经收到了董事会的回复。在我的线程中,我等到 AtomicBoolean 为真,如果不是真的,我让它休眠 200 毫秒。我的问题是有时并非所有包裹都被发送。此外,我有时会陷入 while 循环。你能给我一些建议来提高我的发送性能吗?

发送

 public void sendData(final String text) {
    refreshDeviceCache(bluetoothGatt);
    this.text = text;
    BluetoothGattService bluetoothGattService = bluetoothGatt.getService(UUID.fromString(SampleGattAttributes.UUID_SERVICE_NORDIC));
    final BluetoothGattCharacteristic characteristicRx = bluetoothGatt.getService(UUID.fromString(SampleGattAttributes.UUID_SERVICE_NORDIC)).getCharacteristic(UUID_RX_CHARACTERISTIC);
    byte[] tx = new byte[0];
    tx = text.getBytes(StandardCharsets.UTF_8);
    characteristicRx.setValue(tx);
    Thread t = new Thread(new Runnable() {
        int sendCounter = 0;
        int alert = 0;
        boolean isRunning = true;

        @Override
        public void run() {
            while (isRunning) {
                if (getSendingState.get()) {
                    bluetoothGatt.writeCharacteristic(characteristicRx);
                    getSendingState.set(false);
                    alert=0;
                    if (sendCounter == 19) isRunning = false;
                    else sendCounter++;
                } else {
                    if (alert==2)
                    {
                        getSendingState.set(true);
                        alert=0;
                    }
                    else {
                        alert++;
                        try {
                            Thread.sleep(250);

                        } catch (Exception e) {
                            e.getStackTrace();
                        }
                    }
                }
            }
            
        }
    });
    t.start();


}

恢复:

    if (UUID_TX_CHARACTERISTIC.equals(characteristic.getUuid())) {
        Log.w("NOTIFICATION", Arrays.toString(characteristic.getValue()));
        intent.putExtra(EXTRA_DATA, Arrays.toString(characteristic.getValue()));
        byte[] bytes = characteristic.getValue();
        incomingDataAsString = new String(bytes);
        getSendingState.set(true);
    } // This is your code

班级宣言

  private Binder binder = new LocalBinder();
private BluetoothAdapter bluetoothAdapter;
private BluetoothGatt bluetoothGatt;
private String currentAddress;

//service state variables
private AtomicBoolean serviceBound = new AtomicBoolean(false);
private AtomicBoolean serviceStarted = new AtomicBoolean(false);

//GATT states
public final static String ACTION_GATT_CONNECTED =
        "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
public final static String ACTION_GATT_DISCONNECTED =
        "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
public final static String ACTION_GATT_SERVICES_DISCOVERED =
        "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";

//Service states
public final static String ACTION_DATA_AVAILABLE =
        "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
public final static String EXTRA_DATA =
        "com.example.bluetooth.le.EXTRA_DATA";
public final static UUID UUID_HEART_RATE_MEASUREMENT =
        UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);
public final static UUID UUID_RX_CHARACTERISTIC = UUID.fromString(SampleGattAttributes.UUID_RX_CHARACTERISTIC_NORDIC);
public final static UUID UUID_TX_CHARACTERISTIC = UUID.fromString(SampleGattAttributes.UUID_TX_CHARACTERISTIC_NORDIC);

//TAGS
private String BLUETOOTH_GATT_CALLBACK = "BLUETOOH_GATT_CALLBACK_SERVICE";
private String BLUETOOTH_CONNECTION_INFORMATION = "BLUETOOTH_CONNECTION_INFORMATION_SERVICE";
private String BLUETOOTH_ADAPTER = "BLUETOOTH_ADAPTER_SERVICE";
private String FUNCTIONAL_METHODES = "FUNCTIONAL_METHODES_SERVICE";


//Connection states
private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTED = 2;
private int connectionState;
private boolean NOTIFICATION_ENABLED = false;

//VARIABLES FOR FUNCTIONAL USAGE
private String incomingDataAsString = "";
private AtomicBoolean getSendingState = new AtomicBoolean(true);
private AtomicInteger notificationCounter = new AtomicInteger(0);

亲切的问候,托马斯

标签: android

解决方案


推荐阅读