Android 藍牙設備的查找和鏈接

1.權限 使用藍牙設備須要先在Manifest中開放權限,位置以下。 [html] view plaincopy <manifest ...>
<application ...>
...
</application>html

// 使用藍牙設備的權限  
<uses-permission android:name="android.permission.BLUETOOTH" />  
// 管理藍牙設備的權限  
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

</manifest> java

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);
}android

3.獲取已配對的藍牙設備(android.bluetooth.BluetoothDevice) 首次鏈接某藍牙設備須要先配對,一旦配對成功,該設備的信息會被保存,之後鏈接時無需再配對,因此已配對的設備不必定是能鏈接的。 [java] view plaincopy BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for(int i=0; i<devices.size(); i++)
{
BluetoothDevice device = (BluetoothDevice) devices.iterator().next();
System.out.println(device.getName());
}app

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();socket

自定義廣播類 [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());
}
}
}ide

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;
}
}
}
}
}ui

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();
}code

相關文章
相關標籤/搜索