Android蓝牙开发
BLE开发
版本兼容6.0之后动态申请权限Manifest.permission.ACCESS_COARSE_LOCATION
Android版本21之后的扫描方式改变
private void scanBluetooth(boolean flag){
if (flag){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothAdapter.getBluetoothLeScanner().startScan(scanCallback);
}else {
mBluetoothAdapter.startLeScan(mLeScanCallback);
}
}else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothAdapter.getBluetoothLeScanner().stopScan(scanCallback);
}else {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
}
Android 6.0之后连接方式也发生了改变
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
bluetoothGatt = bleDevice.getDevice().connectGatt(BleManager.getInstance().getContext(),
autoConnect, coreGattCallback, TRANSPORT_LE);
}else {
bluetoothGatt = bleDevice.getDevice().connectGatt(BleManager.getInstance().getContext(),
autoConnect, coreGattCallback);
}