Android藍(lán)牙報133錯誤解決辦法

1.重點在于資源釋放,BluetoothGatt的close方法一定要寫在BluetoothGattCallback的onConnectionStateChange回調(diào)方法的newState為BluetoothProfile.STATE_DISCONNECTED的狀態(tài)下,即調(diào)用BluetoothGatt的disconnect()方法觸發(fā)的BluetoothProfile.STATE_DISCONNECTED狀態(tài)下,否則資源無法釋放.調(diào)用BluetoothGatt的disconnect()方法時一定要在主線程中,否則可能出現(xiàn)關(guān)不掉的可能.

2.下面的為網(wǎng)上都可以找到的

2.1清除藍(lán)牙緩存方法,通過反射機(jī)制調(diào)用,注意的是此方法需要在BluetoothGatt的close方法之前調(diào)用,即調(diào)用了清除緩存的方法,就可以調(diào)用close方法.

/** * Clears the internal cache and forces a refresh of the services from the * remote device. */

public static boolean refreshDeviceCache(BluetoothGatt mBluetoothGatt) {

if (mBluetoothGatt !=null) {

try {

BluetoothGatt localBluetoothGatt = mBluetoothGatt;

Method localMethod = localBluetoothGatt.getClass().getMethod("refresh",new Class[0]);

if (localMethod !=null) {

boolean bool = ((Boolean) localMethod.invoke(localBluetoothGatt,new Object[0])).booleanValue();

return bool;

}

}catch (Exception localException) {

localException.printStackTrace();

Log.i(MyConstant.TAGBLUETOOTH,"An exception occured while refreshing device");

}

}

return false;

}

2.2資源釋放關(guān)鍵地方

@Override

????publicvoidonConnectionStateChange(BluetoothGatt gatt, intstatus, intnewState) {

????????String intentAction;

????????if(status == BluetoothGatt.GATT_SUCCESS) {

????????????if(newState == BluetoothProfile.STATE_CONNECTED) {

????????????????intentAction = BluetoothConstants.ACTION_GATT_CONNECTED;

????????????????mBLEConnectionState = BluetoothConstants.BLE_STATE_CONNECTED;

????????????????broadcastUpdate(intentAction);

????????????????Log.i(TAG, "Connected to GATT server.");

????????????????Log.i(TAG, "Attempting to start service discovery:"+ mBluetoothGatt.discoverServices());

????????????} elseif(newState == BluetoothProfile.STATE_DISCONNECTED) {

????????????????intentAction = BluetoothConstants.ACTION_GATT_DISCONNECTED;

????????????????mBLEConnectionState = BluetoothConstants.BLE_STATE_DISCONNECTED;

????????????????close(); // 防止出現(xiàn)status 133

????????????????Log.i(TAG, "Disconnected from GATT server.");

????????????????broadcastUpdate(intentAction);

????????????}

????????} else{

????????????Log.d(TAG, "onConnectionStateChange received: "+ status);

????????????intentAction = BluetoothConstants.GATT_STATUS_133;

????????????mBLEConnectionState = BluetoothConstants.BLE_STATE_DISCONNECTED;

????????????close(); // 防止出現(xiàn)status 133

????????????broadcastUpdate(intentAction);

????????????connect(reConnectAddress);

????????}

????}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容