亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android BLE 寫入特性鎖定 onCharacteristicWrite/

Android BLE 寫入特性鎖定 onCharacteristicWrite/

大話西游666 2023-10-13 14:53:42
我有一個用于發送消息緩沖區的消息線程。每條消息都會排隊等待發送一次,一旦onCharacteristicWrite成功,該特征就會寫入下一條消息。特性也設置為WRITE_TYPE_NO_RESPONSE,因此特性寫入調用之間的消息緩沖隊列非??欤ù蠹s 0-7 毫秒)。主要問題:“堵塞”特征在大多數情況下,這效果很好。當存在大量消息時,似乎會出現此問題(可能會在消息較少時發生,但在發送大量消息時更明顯)。發生的情況是writeCharacteristicwill 被調用,并且該特性似乎鎖定,因為onCharacteristicChanged不再讀取任何新數據,并且無法訪問onCharacteristicWrite.我注意到的其他事情:每次添加 5-10ms 的睡眠延遲characteristicWrite似乎有幫助,但我不明白為什么藍牙 GATT 對象在成功返回時需要延遲onCharacteristicWrite。onConnectionStateChange有時我會收到狀態 8、設備超出范圍的回調。但這并不總是發生。有時characteristicWrite返回 false;然而,它也可以在進入上述“堵塞特征”狀態之前返回 true消息線程代碼:    private boolean stopMessageThread = false;    private boolean characteristicWriteSuccessful = true;    private ArrayList<byte[]> messageQueue = new ArrayList<byte[]>();    private Thread messageThread =  new Thread( new Runnable() {        private long lastTime = 0;        private int count = 0;        @Override        public void run() {            while (!Thread.currentThread().isInterrupted() && !stopMessageThread) {                if(messageQueue.size() != 0 && characteristicWriteSuccessful) {                    Log.i(TAG, ""+(System.currentTimeMillis()-lastTime));                    Log.i(TAG, "Queue count: "+messageQueue.size());                    characteristicWriteSuccessful = false;                    byte[] message = messageQueue.remove(0);                    customCharacteristic.setValue(message);                    boolean status = bluetoothGatt.writeCharacteristic(customCharacteristic);                    Log.i(TAG, "write characteristic status "+status);                    lastTime = System.currentTimeMillis();                    //sleep(10); // this kinda helps but can still throw the error                }            }        }    });
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

除了繁忙的等待(這會阻塞整個 CPU 并快速耗盡電池)之外,我看不到任何同步。有共享數據結構(可能是stopMessageThread、characteristicWriteSuccessful和messageQueue)和多個訪問它們的線程。如果沒有同步,就會出現競爭條件,并且堵塞可能是其表現。


所以我建議采用更簡單的設計,特別是沒有用于發送消息的線程:


private ArrayList<byte[]> messageQueue = new ArrayList<byte[]>();

private boolean isSending = false;


void sendMessage(byte[] message) {

    synchronized (this) {

        if (isSending) {

            messageQueue.add(message);

            return;

        }

        isSending = true;

    }

    customCharacteristic.setValue(message);

    bluetoothGatt.writeCharacteristic(customCharacteristic);

}


public void onCharacteristicWrite (BluetoothGatt gatt, 

                BluetoothGattCharacteristic characteristic, 

                int status) {

    byte[] message;

    synchronized (this) {

        if (messageQueue.size() == 0) {

            isSending = false;

            return;

        }

        message = messageQueue.remove(0);

    }

    customCharacteristic.setValue(message);

    bluetoothGatt.writeCharacteristic(customCharacteristic); 

}

該解決方案的假設是writeCharacteristic不會阻塞并且速度很快。這是一個安全的假設,因為該方法在設計上是異步的:它有一個回調,將在操作完成時調用。


因此回調onCharacteristicWrite用于發送緩沖區中的下一條消息。因此,對線程的需求消失了——相關的復雜性也消失了。


當從后臺線程調用回調時,仍然涉及多個線程。因此,對共享數據的訪問是同步的。


查看完整回答
反對 回復 2023-10-13
  • 1 回答
  • 0 關注
  • 670 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號