標籤:except ... ati catch xtu try soc ring 停止
學習路線
1 藍芽許可權
<uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> //安卓6.0版本需要位置許可權
2 開啟藍芽
public void onOpen(View v) { bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //擷取藍芽適配器 if (!bluetoothAdapter.isEnabled()) { //如果藍芽是關閉的,則開啟 bluetoothAdapter.enable(); showMessage("Open bluetooth"); } else { showMessage("Bluetooth is already open"); } }
3 掃描周圍藍芽(擷取配對藍芽)
掃描周圍的藍芽需要定義一個廣播接收器
private BroadcastReceiver mReceiver=new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String action=intent.getAction(); Log.i("boye", action); if(action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //擷取當前掃描到的裝置 if(device.getBondState()==BluetoothDevice.BOND_BONDED) { //顯示已配對裝置 textView.append("\n"+device.getName()+"==>"+device.getAddress()+"\n"); Toast.makeText(context, "發現已配對裝置:" + device.getName(), Toast.LENGTH_LONG).show(); } else if(device.getBondState()!=BluetoothDevice.BOND_BONDED) { textView3.append("\n"+device.getName()+"==>"+device.getAddress()+"\n"); Toast.makeText(context, "發現未配對裝置:" + device.getName(), Toast.LENGTH_LONG).show(); } } else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ textView2.setText("搜尋完成..."); } } };
廣播過濾器
IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); //過濾出發現registerReceiver(mReceiver,filter); //註冊廣播IntentFilter filter2=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //過濾出掃描結束registerReceiver(mReceiver,filter2); //註冊廣播
4 藍芽配對
public void createBond(BluetoothDevice btDev, Handler handler) { if (btDev.getBondState() == BluetoothDevice.BOND_NONE) { //如果這個裝置取消了配對,則嘗試配對 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //安卓版本大於等於4.3 btDev.createBond(); } } else if (btDev.getBondState() == BluetoothDevice.BOND_BONDED) { //如果這個裝置已經配對完成,則嘗試串連 connect(btDev, handler); } }
5 藍芽的串連
private void connect(BluetoothDevice btDev, Handler handler) { try { //通過和伺服器協商的uuid來進行串連 mBluetoothSocket = btDev.createRfcommSocketToServiceRecord(BltContant.SPP_UUID); if (mBluetoothSocket != null) //全域只有一個bluetooth,所以我們可以將這個socket對象儲存在appliaction中 BltAppliaction.bluetoothSocket = mBluetoothSocket; Log.i("blueTooth", "開始串連..."); //在建立之前調用 if (getmBluetoothAdapter().isDiscovering()) //停止搜尋 getmBluetoothAdapter().cancelDiscovery(); //如果當前socket處於非串連狀態則調用串連 if (!getmBluetoothSocket().isConnected()) { //你應當確保在調用connect()時裝置沒有執行搜尋裝置的操作。 // 如果搜尋裝置也在同時進行,那麼將會顯著地降低串連速率,並很大程度上會串連失敗。 getmBluetoothSocket().connect(); } Log.i("blueTooth", "已經串連"); if (handler == null) return; //結果回調 Message message = new Message(); message.what = 4; message.obj = btDev; handler.sendMessage(message); } catch (Exception e) { Log.i("blueTooth", "...串連失敗"); try { getmBluetoothSocket().close(); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } }
6 藍芽通訊
(1)發送訊息
public static void sendMessage(String message) { if (BltAppliaction.bluetoothSocket == null || TextUtils.isEmpty(message)) return; try { //message += "\n"; OutputStream outputStream = BltAppliaction.bluetoothSocket.getOutputStream(); outputStream.write(message.getBytes("utf-8")); outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } }}
(2)接收訊息
public static void receiveMessage(Handler handler) { if (BltAppliaction.bluetoothSocket == null || handler == null) return; try { InputStream inputStream = BltAppliaction.bluetoothSocket.getInputStream(); byte[] buffer = new byte[200]; inputStream.read(buffer); Log.i("boye收到的資訊",new String(buffer)); Message message = new Message(); message.obj = new String(buffer); message.what = 1; handler.sendMessage(message); } catch (IOException e) { e.printStackTrace(); } }
android藍芽學習