Home > Mobile >  Fragments in the face of the bluetooth operation, for the first time can run normally, but start aga
Fragments in the face of the bluetooth operation, for the first time can run normally, but start aga

Time:09-25

Enter into fragments:
 BluetoothManager BluetoothManager; 
BluetoothManager=(bluetoothManager) mContext. GetSystemService (Context. BLUETOOTH_SERVICE);
BluetoothAdapter=bluetoothManager. GetAdapter ();
{if (bluetoothAdapter. IsEnabled ())
BluetoothAdapter. StartLeScan (mLeScanCallback);
}


Correction:
 
Private final BluetoothAdapter. LeScanCallback mLeScanCallback=new LeScanCallBackClass (this);

Private static final class LeScanCallBackClass implements BluetoothAdapter. LeScanCallback {

WeakReference that;

LeScanCallBackClass (ZhongJieFragment saFragment) {
That=new WeakReference<> (saFragment);
}

@ Override
Public void onLeScan (BluetoothDevice device, int rssi, byte [] scanRecord) {
If (device!=null & amp; & Device. The getName (). The startsWith (" BLE device ")) {
//LogUtils. D (TAG, "found that equipment");
Final ZhongShengZhongJieFragment out=that. The get ();
Out. The bluetoothAdapter. StopLeScan (out. MLeScanCallback);
Out. BluetoothDevice=device;
The Message the Message=Message. Obtain ();
Message. What=MSG_HANDLER_CONNECTDEVICE;
Out. The handler. SendMessage (message);
}
}
}




Exit the fragments of time
 
@ Override
Public void onDestroy () {
Super. OnDestroy ();

If (bluetoothAdapter!=null) {
BluetoothAdapter. StopLeScan (mLeScanCallback);

BluetoothAdapter. Disable ();

}
If (bluetoothGatt!=null) {
RefreshDeviceCache ();
}
If (bluetoothGatt!=null) {
BluetoothGatt. Close ();
}

BluetoothAdapter=null;
CoreGattCallback=null;
The Runtime. GetRuntime (). The gc ();
}




  • Related