近期項(xiàng)目中需要連接藍(lán)牙設(shè)備,起初只是設(shè)置藍(lán)牙列表界面讓用戶點(diǎn)擊然后輸入默認(rèn)PIN碼,后來(lái)改需求了 = = ,要求自動(dòng)連接指定設(shè)備并不需要用戶手動(dòng)輸入PIN碼,作為Android 小白的我是拒絕的,但是拒絕有什么用~
首先說(shuō)一下之后會(huì)用到的關(guān)于藍(lán)牙方面的東西:
1.斷開(kāi)已配對(duì)設(shè)備
最后在項(xiàng)目中發(fā)現(xiàn)沒(méi)有用。這里就先記錄一下。
//得到配對(duì)的設(shè)備列表,清除已配對(duì)的設(shè)備 public void removePairDevice() { if (mBluetoothAdapter != null) { //mBluetoothAdapter初始化方式 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //這個(gè)就是獲取已配對(duì)藍(lán)牙列表的方法 Set<BluetoothDevice> bondedDevices = mBluetoothAdapter.getBondedDevices(); for (BluetoothDevice device : bondedDevices) { //這里可以通過(guò)device.getName() device.getAddress()來(lái)判斷是否是自己需要斷開(kāi)的設(shè)備 unpairDevice(device); } } } //反射來(lái)調(diào)用BluetoothDevice.removeBond取消設(shè)備的配對(duì) private void unpairDevice(BluetoothDevice device) { try { Method m = device.getClass().getMethod("removeBond", (Class[]) null); m.invoke(device, (Object[]) null); } catch (Exception e) { Log.e("mate", e.getMessage()); } }2.搜索附近藍(lán)牙
首先我們需要注冊(cè)兩個(gè)廣播,第一個(gè)為正在搜索時(shí)的,第二個(gè)為搜索完成的。
// Register for broadcasts when a device is discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(mFindBlueToothReceiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(mFindBlueToothReceiver, filter); //需要時(shí)開(kāi)始搜索 if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } mBluetoothAdapter.startDiscovery();然后對(duì)廣播進(jìn)行處理。這里要說(shuō)明一下ClsUtils.createBond()這個(gè)方法如果連接設(shè)備SDK中有配對(duì)的方法,建議把這個(gè)方法去掉,我這里是去掉的,加上的話偶爾會(huì)Toast出無(wú)法配對(duì)。
private final BroadcastReceiver mFindBlueToothReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { //TODO 開(kāi)始搜索 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // if (device.getBondState() != BluetoothDevice.BOND_BONDED) {//判斷藍(lán)牙狀態(tài),是否是已配對(duì) //TODO 可以在這判斷名字 如果搜索結(jié)束后沒(méi)有,再到已配對(duì)中尋找 String BTName[] = device.getName().split("-"); if (BTName[0].equals("xxx")) { //在這連接設(shè)備 一般需要藍(lán)牙地址 device.getAddress(); try { ClsUtils.createBond(device.getClass(), device); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // } } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { //TODO 搜索結(jié)束 Toast.makeText(context, "搜索結(jié)束",Toast.LENGTH_SHORT).show(); } } };這里在Activity結(jié)束時(shí)記得取消注冊(cè)和取消搜索
unregisterReceiver(mFindBlueToothReceiver); if (mBluetoothAdapter != null) { mBluetoothAdapter.cancelDiscovery(); }3.攔截用戶交互頁(yè)面
通過(guò)廣播可以監(jiān)聽(tīng)到輸入PIN碼的那個(gè)頁(yè)面將要彈出
<receiver android:name=".BluetoothConnectActivityReceiver" > <intent-filter android:priority="1000"> <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> </intent-filter> </receiver>
廣播中需要做的事情,注意一定要調(diào)用abortBroadcast(),不然交互頁(yè)面還是會(huì)出現(xiàn)一下然后消失。就是這個(gè)方法找了一天( 主站蜘蛛池模板: 会泽县| 静安区| 白山市| 邛崃市| 若羌县| 蒙城县| 禄丰县| 个旧市| 辽阳市| 娄烦县| 乌拉特中旗| 景德镇市| 思茅市| 遂溪县| 滨州市| 时尚| 纳雍县| 丘北县| 灵丘县| 上犹县| 两当县| 如皋市| 那坡县| 石屏县| 平凉市| 河间市| 宣武区| 军事| 当雄县| 田东县| 穆棱市| 西吉县| 商水县| 原平市| 南宁市| 津南区| 九台市| 武汉市| 乳山市| 麻城市| 太仓市|