android BLE 40 setCharacteristicNotification接收不到數據


參考連接:http://www.codes51.com/article/detail_4021799.html

 

問題描述 最近在開發android BLE  讀寫數據

但是向設備寫數據很順利,但是在接收設備傳來的數據時,死活接收不到.

 /**
     * Enables or disables notification on a give characteristic.
     *
     * @param characteristic Characteristic to act on.
     * @param enabled        If true, enable notification.  False otherwise.
     */
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    
    }

上面這樣寫 是收不到數據的 ,主要是少加了mBluetoothGatt.writeDescriptor(descriptor);

下面是修改后的代碼

   /**
     * Enables or disables notification on a give characteristic.
     *
     * @param characteristic Characteristic to act on.
     * @param enabled        If true, enable notification.  False otherwise.
     */
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
//        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        boolean isEnableNotification =  mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        if(isEnableNotification) {
            List<BluetoothGattDescriptor> descriptorList = characteristic.getDescriptors();
            if(descriptorList != null && descriptorList.size() > 0) {
                for(BluetoothGattDescriptor descriptor : descriptorList) {
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    mBluetoothGatt.writeDescriptor(descriptor);
                }
            }
        }
    }

 如果 不是這種 情況接收不到數據就是另外一種可能了characteristic的 uuid設置不對

你可以看下 boolean isEnableNotification = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

返回的是false 還是true,

另外還會再寫一篇關於藍牙數據讀寫的文章

 

以上就介紹了 android BLE 40 setCharacteristicNotification接收不到數據,包括了方面的內容,希望對Android開發有興趣的朋友有所幫助。

本文網址鏈接:http://www.codes51.com/article/detail_4021799.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM