BLE、Fragment和Activity
将这周弄的一些东西记录下吧
- BLE
在这个部分的认识过程中,首要了解的是交互过程:
①蓝牙设备相关:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
BluetoothManager bluetoothManager = (BluetoothManager) helperContext.getSystemService(BLUETOOTH_SERVICE); if (bluetoothManager != null) { bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter != null) { bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); } }
②蓝牙le扫描:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void search() throws InterruptedException { Log.d(TAG, "蓝牙正在扫描..."); if (isSearching) return ; bluetoothDevicesList.clear(); bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); if (bluetoothLeScanner != null) { bluetoothLeScanner.startScan(scanCallback); isSearching = true; } }
其中的scanCallback是一个ScanCallback回调对象(扫描要设置回调对象),要重写她的几个方法,其中OnScanResult如下示例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Override public void onScanResult(int callbackType, ScanResult result) { BluetoothDevice bluetoothDevice = result.getDevice(); String name = result.getDevice().getName(); String address = result.getDevice().getAddress(); int status = result.getDevice().getBondState(); Log.d(TAG, "onScanRusult: " + name + " " + address + " "+status+' '); if (name != null && !bluetoothDevicesList.contains(bluetoothDevice)) bluetoothDevicesList.add(bluetoothDevice); raiseonScanning(); // notify的作用 }
如果扫描失败了,会触发OnScanFailed,在里面可以将蓝牙重启。
③蓝牙le连接
这个时候:
public void connectGatt() { if (bluetoothAdapter == null || bluetoothDevice == null) return ; bluetoothGatt = bluetoothDevice.connectGatt(helperContext, false, gattCallback); }
其中bluetoothDevice是在bluetoothDeviceLists中的一个,使用该对象的connectGatt方法,参数分别为上下文环境、是否自动重新连接、连接回调函数
其中的gattCallback是关键,来往BLE设备的信息都经过她,她是一个BluetoothGattCallback对象,要重写她的相应方法,
拿public void onConnectionStateChange(BluetoothGatt gatt, int status, final int newState)来说明:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, final int newState) { helperHandler.post(new Runnable() { @Override public void run() { switch (newState) { case BluetoothGatt.STATE_CONNECTED: isConnectedGatt = true; bluetoothLeScanner.stopScan(scanCallback); bluetoothGatt.discoverServices(); // 连接后要发现有什么服务 raiseonConnected(); break; case BluetoothGatt.STATE_DISCONNECTED: stopTimer(); raiseonDisconnected(); isConnectedGatt = false; isMeasuring = false; break; case BluetoothGatt.STATE_CONNECTING: raiseonConnecting(); break; case BluetoothGatt.STATE_DISCONNECTING: raiseonDisconnecting(); isMeasuring = false; break; } } }); super.onConnectionStateChange(gatt, status, newState); }
包括Characteristic读、写、变化,Descriptor读写,服务发现,在使用的时候结合自己的需求
其中的GATT的相关要自行了解
④对Characteristic、Descriptor的写值来和BLE设备交互
这里只说一点要注意的,在需要设备回传值时,写值时一定要setCharacteristicNotification(myGatCharacter, true),否则读不出值的额。
- Fragment和Activity数据传递
①在Fragment设置接口,在宿主Activity中实现,在Fragment的OnAttach阶段传递上下文环境(发消息给宿主Activity)
②Fragment之间的数据交流中间人是宿主Activity(想想①就知道了)
完