本文是基于Android Studio下的Bluetooth的相关开发,记录一些开发笔记。
API基础
BluetoothAdapter——本地蓝牙适配器 BluetoothAdapter是所有蓝牙交互的入口,通过这个类,能够发现其他的蓝牙设备,查询已配对设备的列表,使用已知的MAC地址来实例化一个BluetoothDevice对象,并且创建一个BluetoothServerSocket对象来监听与其他设备的通信。
BluetoothDevice——远程的蓝牙设备 使用这个类通过BluetoothSocket或查询诸如名称、地址、类和配对状态等设备信息来请求跟远程设备的连接。
BluetoothSocket——蓝牙socket接口 和TCPSocket类似,允许一个应用程序跟另一个蓝牙设备通过输入流和输出流进行数据交换的连接点。
BluetoothServerSocket——监听传入请求服务接口 和TCPServerSocket类似,等待连接当远程蓝牙设备请求跟本设备建立连接请求时,BluetoothServerSocket会在连接被接收时返回一个被连接的BluetoothSocket对象。
蓝牙权限
在调用Bluetooth的各种API之前先要赋予应用使用Bluetooth的各种权限,一般只需要这两个权限。
android.permission.BLUETOOTH : 允许程序连接到已配对的蓝牙设备, 请求连接/接收连接/传输数据需要改权限, 主要用于对配对后进行操作。
android.permission.BLUETOOTH_ADMIN : 允许程序发现和配对蓝牙设备, 该权限用来管理蓝牙设备, 有了这个权限, 应用才能使用本机的蓝牙设备, 主要用于对配对前的操作。
在进行调用之前一定要记得在AndroidManifest.xml中授权,如下:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
初始化蓝牙
利用BluetoothAdapter的getDefaultAdapter()方法初始化蓝牙
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
判断设备是否支持蓝牙
通过上面的适配器的初始化是否成功复制来判断设备是否支持蓝牙
public void initBluetooth() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Log.d(TAG, "notSupport");
} else {
Log.d(TAG, "Support");
}
}
启动蓝牙
调用BluetoothAdapter的enable()方法即可开启蓝牙
public void openBluetooth() {
boolean openResult = bluetoothAdapter.enable();
if (openResult) {
Log.d(TAG, "start");
} else {
Log.d(TAG, "false");
}
}
搜索外部设备
调用startDiscovery()方法即可开始搜索外部设备,startDiscovery()方法调用的时候默认执行下面的onReceive()方法,具体的参数获取在onReceive()方法中实现。(需要注意的是onReceive()方法中不能执行耗时操作,因为onReceive()的生命周期很短,有可能广播接收者结束,子线程还没有结束,这时广播接收者所在的进程有可能被系统清理掉。)startDiscovery()方法是一个异步方法,它会对其他蓝牙设备进行搜索,持续时间为12秒,搜索过程其实是在System Service中进行,可以通过cancelDiscovery()方法来停止这个搜索。
private void startDiscovery() {
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.startDiscovery();
} else {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
}
注册广播
在执行onReceive()方法之前,一定要记得先注册广播,否则无法正常调用onReceive()方法。
public void registerBoard() {
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(broadcastReceiver, filter);
}
在onReceive中处理广播
在这里分别获取外部蓝牙设备的name、address(mac地址)以及rssi(信号强度),设置一个String变量接收广播信号,共有三种广播信号,ACTION_DISCOVERY_START(开始搜索),ACTION_DISCOVERY_FINISHED(搜索结束)和ACTION_FOUND(找到设备)
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device;
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name, address, rssi, deviceString = "";
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
name = device.getName();
address = device.getAddress();
rssi = Integer.toString(intent.getShortExtra
(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE));
if (!bluetoothAddresses.contains(address)) {
bluetoothAddresses.add(address);
if (name == null || name.equals("")) {
deviceString = address + " - RSSI " + rssi + "dBm";
} else {
deviceString = name + " - RSSI " + rssi + "dBm";
}
bluetoothDevices.add(deviceString);
}
Log.e(TAG, "-------------------");
Log.e(TAG, "discover:" + deviceString);
}
} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
Log.e(TAG, "End");
bluetoothAdapter.cancelDiscovery();
}
}
};
其他
还有许多其他功能,例如:设置蓝牙的可见性、关闭蓝牙、获取蓝牙的状态、与特定设备建立连接、蓝牙模块的通信套接字编程等,不一一赘述。