1.权限
使用蓝牙设备需要先在Manifest中开放权限,位置如下。
- <manifest ...>
- <application ...>
- ...
- </application>
-
- // 使用蓝牙设备的权限
- <uses-permission android:name="android.permission.BLUETOOTH" />
- // 管理蓝牙设备的权限
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
- </manifest>
2.打开蓝牙
获得蓝牙适配器(android.bluetooth.BluetoothAdapter),检查该设备是否支持蓝牙,如果支持,就打开蓝牙。
- adapter = BluetoothAdapter.getDefaultAdapter();
- if (adapter == null)
- {
-
- }
- if (!adapter.isEnabled())
- {
- Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
-
- intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
- context.startActivity(intent);
- }
3.获取已配对的蓝牙设备(android.bluetooth.BluetoothDevice)
首次连接某蓝牙设备需要先配对,一旦配对成功,该设备的信息会被保存,以后连接时无需再配对,所以已配对的设备不一定是能连接的。
- 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());
- }
4.搜索周围的蓝牙设备
适配器搜索蓝牙设备后将结果以广播形式传出去,所以需要自定义一个继承广播的类,在onReceive方法中获得并处理蓝牙设备的搜索结果。
- 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);
- adapter.startDiscovery();
自定义广播类
- 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.蓝牙设备的配对和状态监视
- 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.蓝牙设备的连接
- private void connect(BluetoothDevice device) throws IOException {
-
- final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
- UUID uuid = UUID.fromString(SPP_UUID);
- BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
- socket.connect();
- }
Android 蓝牙设备的查找和连接
原文:http://www.cnblogs.com/pinksnow520/p/4158234.html