Android 蓝牙相关
本文主要讲述android 蓝牙的简单使用.
1: 是否支持蓝牙
/*** 是否支持蓝牙** @return*/
private boolean isSupportBluetooth() {BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();return bluetoothAdapter != null;
}
2: 开启蓝牙
注意这里需要申请下BLUETOOTH 权限.
/*** 开启蓝牙*/
private void openBluetooth() {BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//RequiresPermission(Manifest.permission.BLUETOOTH)if (!bluetoothAdapter.isEnabled()) {Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableBtIntent, 1001);}
}
点击跳转后的界面如下:
3: 关闭蓝牙
private void closeBluetooth(){BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (bluetoothAdapter.isEnabled()) {bluetoothAdapter.disable();}
}
关闭蓝牙同样会有弹框提示,如下:
4: 扫描蓝牙设备
这里其实分为两部分 发现设备以及查找已配对设备.
首先,我们先讲下发现设备,这里需要调用bluetoothAdapter.startDiscovery(). 该进程是异步的,并返回一个布尔值,指示发现是否已成功启动。发现过程通常涉及大约 12 秒的查询扫描,随后会对发现的每台设备进行页面扫描,以检索其蓝牙名称。
其次我们需要注册相关的broadcast ,来接收扫描的结果.
具体的代码如下:
private final BroadcastReceiver receiver = new BroadcastReceiver() {public void onReceive(Context context, Intent intent) {String action = intent.getAction();if (BluetoothDevice.ACTION_FOUND.equals(action)) {// Discovery has found a device. Get the BluetoothDevice// object and its info from the Intent.BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);String deviceName = device.getName();String deviceHardwareAddress = device.getAddress(); // MAC addressLog.d(TAG, "onReceive: deviceName:"+deviceName+"; deviceHardwareAddress:"+deviceHardwareAddress);}}
};
private void registerBluetoothCast(){// Register for broadcasts when a device is discovered.//Requires {@link android.Manifest.permission#BLUETOOTH} and//{@link android.Manifest.permission#ACCESS_COARSE_LOCATIONIntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);registerReceiver(receiver, filter);
}
需要注意的是权限上需要注册:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
//发现设备
Log.d(TAG, "onClick: startDiscovery");
bluetoothAdapter.startDiscovery();
执行后具体的输出如下:
2023-12-12 14:34:33.706 11287-11287/com.test.bluetooth D/bluetoothAdapter: onClick: startDiscovery
2023-12-12 14:34:33.706 11287-11287/com.test.bluetooth D/BluetoothAdapter: startDiscovery(): called by: com.test.bluetooth
2023-12-12 14:34:33.707 11287-11287/com.test.bluetooth D/BluetoothAdapter: 210428169: getState(). Returning ON
2023-12-12 14:34:33.779 11287-11287/com.test.bluetooth D/bluetoothAdapter: onReceive: deviceName:vivo TWS 2; deviceHardwareAddress:CC:xx:xx:xx:xx:92
2023-12-12 14:34:33.780 11287-11287/com.test.bluetooth D/bluetoothAdapter: onReceive: deviceName:null; deviceHardwareAddress:3F:xx:xx:xx:xx:90
2023-12-12 14:34:34.052 11287-11287/com.test.bluetooth D/bluetoothAdapter: onReceive: deviceName:OPPO A95 5G; deviceHardwareAddress:30:xx:xx:xx:xx:9C
2023-12-12 14:34:34.351 11287-11287/com.test.bluetooth D/bluetoothAdapter: onReceive: deviceName: iMac; deviceHardwareAddress:88:xx:xx:xx:xx:BF
2023-12-12 14:34:34.366 11287-11287/com.test.bluetooth D/bluetoothAdapter: onReceive: deviceName:MacBook Pro; deviceHardwareAddress:74:xx:xx:xx:xx:49
获取已配对设备:
private void getBindDevices(){Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();if (pairedDevices.size() > 0) {// There are paired devices. Get the name and address of each paired device.for (BluetoothDevice device : pairedDevices) {String deviceName = device.getName();String deviceHardwareAddress = device.getAddress(); // MAC addressLog.d(TAG, "bind: deviceName:"+deviceName+"; deviceHardwareAddress:"+deviceHardwareAddress);}}else{Log.d(TAG, "bind devices 0");}}
2023-12-12 14:47:21.792 12229-12229/com.test.bluetooth D/BluetoothAdapter: 258056678: getState(). Returning ON
2023-12-12 14:47:21.793 12229-12229/com.test.bluetooth D/bluetoothAdapter: bind: deviceName:夏沫; deviceHardwareAddress:50:xx:xx:xx:xx:5A