進入fragment的時候:
BluetoothManager bluetoothManager;
bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.startLeScan(mLeScanCallback);
}
回呼:
private final BluetoothAdapter.LeScanCallback mLeScanCallback = new LeScanCallBackClass(this);
private static final class LeScanCallBackClass implements BluetoothAdapter.LeScanCallback {
WeakReference<ZhongJieFragment> that;
LeScanCallBackClass(ZhongJieFragment saFragment) {
that = new WeakReference<>(saFragment);
}
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
if (device != null && device.getName().startsWith("BLE Device")) {
//LogUtils.d(TAG,"發現設備");
final ZhongShengZhongJieFragment out = that.get();
out.bluetoothAdapter.stopLeScan(out.mLeScanCallback);
out.bluetoothDevice = device;
Message message = Message.obtain();
message.what = MSG_HANDLER_CONNECTDEVICE;
out.handler.sendMessage(message);
}
}
}
退出fragment 的時候
@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;
Runtime.getRuntime().gc();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/72480.html
標籤:Android
上一篇:QGraphicsPixmapItem和 QGraphicsRectItem物件的setRotation行為不一樣嗎?
下一篇:Ui怎么設計
