Android ble简单使用(初始化)

Haidee ·
更新时间:2024-09-21
· 988 次阅读

网上已经有很多优秀的开源框架了,为了熟悉ble的整个流程还是要自己写一下的。

蓝牙权限 动态申请权限
因为这里只有一个权限需要动态申请,所以用下面的代码 //检查权限是否打开 private void checkPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_COARSE_LOCATION)) { //彻底拒绝 toSelfSetting(activity); } else { //未彻底拒绝 ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1); } } } } //用户操作回调 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 1) { for (int i = 0; i < permissions.length; i++) { if (grantResults[i] == PackageManager.PERMISSION_GRANTED){ //申请成功 }else { toSelfSetting(activity); } } } } //跳转到应用设置页面 private void toSelfSetting(Context context) { Intent mIntent = new Intent(); mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); mIntent.setData(Uri.fromParts("package", context.getPackageName(), null)); context.startActivity(mIntent); } 获取蓝牙适配器

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

判断设备是否支持蓝牙

if (bluetoothAdapter == null) {//为空不支持
return;
}

注册蓝牙广播监听蓝牙状态 private void register() { if (receiver == null) { receiver = new BTStateReceiver(); } receiver.setCallback(this); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF"); intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON"); activity.registerReceiver(receiver, intentFilter); } private void unregister() { if (receiver != null) { activity.unregisterReceiver(receiver); receiver = null; } } BTStateReceiver 的代码 public class BTStateReceiver extends BroadcastReceiver { private BTStateCallback callback; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (action == null) { return; } switch (action) { case BluetoothDevice.ACTION_ACL_CONNECTED: break; case BluetoothDevice.ACTION_ACL_DISCONNECTED: break; case BluetoothAdapter.ACTION_STATE_CHANGED: int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0); switch (blueState) { case BluetoothAdapter.STATE_OFF: callback.onBTOff(); break; case BluetoothAdapter.STATE_ON: Toast.makeText(context, "蓝牙已开启", Toast.LENGTH_SHORT).show(); break; } break; } } public void setCallback(BTStateCallback callback) { this.callback = callback; } public interface BTStateCallback { void onBTOff(); } }

当前只实现蓝牙被关闭时候的操作

实现回调
前面注册广播的时候调用了setCallback的方法,所以当前类需要 implements BTStateCallback 实现这个里面的接口 @Override public void onBTOff() { openBT(); } private void openBT() { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } 注册完广播之后 判断是否打开蓝牙

if (!bluetoothAdapter.isEnabled()) {
openBT();
}

当前初始化已经完成

下一篇说一下扫描吧,堆在一起很乱!链接


作者:qq313629058



ble 初始化 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号