栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Android Studio中配对蓝牙设备

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Android Studio中配对蓝牙设备

首次请求

BLUETOOTH_ADMIN
权限。

然后使您的设备可发现:

private void makeDiscoverable() {        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);        startActivity(discoverableIntent);        Log.i("Log", "Discoverable ");    }

然后创建一个 BroadcastReceiver 来监听系统中的动作:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) { Message msg = Message.obtain(); String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)){    //Found, add to a device list }        }    };

并通过注册以下 BoardcastReceiver 开始搜索设备:

 private void startSearching() {        Log.i("Log", "in the start searching method");        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);        BluetoothDemo.this.registerReceiver(myReceiver, intentFilter);        bluetoothAdapter.startDiscovery();    }

设备从 BroadcastReceiver 进入列表后,从列表中选择设备,并

createBond()
执行以下操作:

 public boolean createBond(BluetoothDevice btDevice)      throws Exception      {         Class class1 = Class.forName("android.bluetooth.BluetoothDevice");        Method createBondMethod = class1.getMethod("createBond");          Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);          return returnValue.booleanValue();      }

Then use your pre above to manage with bonded devices.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号