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