Android 藍牙設備的查找和連接
1.權限
使用藍牙設備需要先在Manifest中開放權限,位置如下。
[html] view plaincopy
<application ...> ... </application> // 使用藍牙設備的權限 <uses-permission android:name="android.permission.BLUETOOTH" /> // 管理藍牙設備的權限 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
2.打開藍牙
獲得藍牙適配器(android.bluetooth.BluetoothAdapter),檢查該設備是否支持藍牙,如果支持,就打開藍牙。
[java] view plaincopy
// 檢查設備是否支持藍牙
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null)
{
// 設備不支持藍牙
}
// 打開藍牙
if (!adapter.isEnabled())
{
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); // 設置藍牙可見性,最多300秒 intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); context.startActivity(intent);
}
3.獲取已配對的藍牙設備(android.bluetooth.BluetoothDevice)
首次連接某藍牙設備需要先配對,一旦配對成功,該設備的信息會被保存,以后連接時無需再配對,所以已配對的設備不一定是能連接的。
[java] view plaincopy
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set devices = adapter.getBondedDevices();
for(int i=0; i {
BluetoothDevice device = (BluetoothDevice) devices.iterator().next(); System.out.println(device.getName());
}
4.搜索周圍的藍牙設備
適配器搜索藍牙設備后將結果以廣播形式傳出去,所以需要自定義一個繼承廣播的類,在onReceive方法中獲得并處理藍牙設備的搜索結果。
[java] view plaincopy
// 設置廣播信息過濾
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
// 注冊廣播接收器,接收并處理搜索結果
context.registerReceiver(receiver, intentFilter);
// 尋找藍牙設備,android會將查找到的設備以廣播形式發出去
adapter.startDiscovery();
自定義廣播類
[java] view plaincopy
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); System.out.println(device.getName()); }
}
}
5.藍牙設備的配對和狀態監視
[java] view plaincopy
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { // 獲取查找到的藍牙設備 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); System.out.println(device.getName()); // 如果查找到的設備符合要連接的設備,處理 if (device.getName().equalsIgnoreCase(name)) { // 搜索藍牙設備的過程占用資源比較多,一旦找到需要連接的設備后需要及時關閉搜索 adapter.cancelDiscovery(); // 獲取藍牙設備的連接狀態 connectState = device.getBondState(); switch (connectState) { // 未配對 case BluetoothDevice.BOND_NONE: // 配對 try { Method createBondMethod = BluetoothDevice.class.getMethod("createBond"); createBondMethod.invoke(device); } catch (Exception e) { e.printStackTrace(); } break; // 已配對 case BluetoothDevice.BOND_BONDED: try { // 連接 connect(device); } catch (IOException e) { e.printStackTrace(); } break; } } } else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { // 狀態改變的廣播 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getName().equalsIgnoreCase(name)) { connectState = device.getBondState(); switch (connectState) { case BluetoothDevice.BOND_NONE: break; case BluetoothDevice.BOND_BONDING: break; case BluetoothDevice.BOND_BONDED: try { // 連接 connect(device); } catch (IOException e) { e.printStackTrace(); } break; } } } }
}
6.藍牙設備的連接
[java] view plaincopy
private void connect(BluetoothDevice device) throws IOException {
// 固定的UUID final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB"; UUID uuid = UUID.fromString(SPP_UUID); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid); socket.connect();
}