usespermission android nameandroid permission BLUETOOTH usespermission android nameandroid
藍芽設定 需要權限 允許應用程式連接藍芽 <uses-permission android: name="android. permission. BLUETOOTH"/> 允許應用程式找到與之連接的藍芽設備 <uses-permission android: name="android. permission. BLUETOOTH_ADMIN"/>
藍芽搜尋 宣告 private Bluetooth. Adapter m. Bluetooth. Adapter; 藍牙適配器 確認藍芽有無打開 m. Bluetooth. Adapter = Bluetooth. Adapter. get. Default. Adapter(); 獲得藍芽適配器 if (!m. Bluetooth. Adapter. is. Enabled()) { Toast. make. Text(get. Application. Context(), "Bluetooth Disabled !", Toast. LENGTH_SHORT). show(); } if (m. Bluetooth. Adapter == null) { Toast. make. Text(get. Application. Context(), "Bluetooth null !", Toast. LENGTH_SHORT) . show(); } else msg. set. Text("Bluetooth OK");
藍芽連結 宣告 private Bluetooth. Socket bt. Socket = null; 藍芽通訊 private static final UUID MY_UUID = UUID. from. String(“ 00001101 -0000 -1000 -8000 - 00805 F 9 B 34 FB”); 辨識碼 連結 Bluetooth. Device device = m. Bluetooth. Adapter. get. Remote. Device(address); msg. set. Text("Connecting to. . . " + device); m. Bluetooth. Adapter. cancel. Discovery(); try { bt. Socket = device. create. Rfcomm. Socket. To. Service. Record(MY_UUID); msg. set. Text("Set Socket "); bt. Socket. connect(); msg. set. Text("Connect Socketed "); } catch (IOException e) { try { bt. Socket. close(); } catch (IOException e 2) { } msg. set. Text("Fail Socket "); }
- Slides: 4