美文网首页
Android蓝牙开发

Android蓝牙开发

作者: 王冥 | 来源:发表于2019-08-05 18:29 被阅读0次

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);

}

相关文章

网友评论

      本文标题:Android蓝牙开发

      本文链接:https://www.haomeiwen.com/subject/ndhvdctx.html