蓝牙设置种常用的Intent
下面是在bluetoothsettings.java 中注册蓝牙Intent的函数:
private boolean initBluetoothAPI() {
mIntentFilter =
// 跟远端蓝牙设备连接上时返回来的intent
new IntentFilter(BluetoothIntent.REMOTE_DEVICE_CONNECTED_ACTION);
// 跟远端蓝牙设备断开时返回来的intent mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_DISCONNECTED_ACTION);
// 跟远端的蓝牙设备配对上时收到的intent,不过前提是对方主动发起的配对
// 才能收到这个intent
mIntentFilter.addAction(BluetoothIntent.BONDING_CREATED_ACTION);
// 本地蓝牙设备可用时收到的Intent
mIntentFilter.addAction(BluetoothIntent.ENABLED_ACTION);
// 本地蓝牙设备不可用时收到的Intent
mIntentFilter.addAction(BluetoothIntent.DISABLED_ACTION);
// 扫描到远端设备时收到的intent mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION);
// 远端蓝牙设备消失时收到的intent mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION);
// 远端蓝牙设备名称更换时收到的intent,因为刚发现设备的时候还没有获取// 它的名称
mIntentFilter.addAction(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION);
// 当有远端设备发起配对请求时收到的intent
mIntentFilter.addAction(BluetoothIntent.PAIRING_REQUEST_ACTION);
// 蓝牙耳机状态改变时候到的intent
mIntentFilter.addAction(BluetoothIntent.HEADSET_STATE_CHANGED_ACTION);
// 扫描设备结束
mIntentFilter.addAction(BluetoothIntent.DISCOVERY_COMPLETED_ACTION);
// 扫描开始
mIntentFilter.addAction(BluetoothIntent.DISCOVERY_STARTED_ACTION);
// 蓝牙设备模式改变,表示本地蓝牙设备是否可以被查找
mIntentFilter.addAction(BluetoothIntent.MODE_CHANGED_ACTION);
// 有耳机插入
mIntentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
启动蓝牙
在启动蓝牙的时候,要注意的地方是不能正常启动蓝牙的情况,因为正常启动的时候会返回BluetoothIntent.ENABLED_ACTION 这个Intent,当时当启动出现异常的时候是没有Intent返回的,android使用回调函数来解决这个问题。下面是在bluetoothdeviceservice.java 里面enable((IBluetoothDeviceCallback callback) 的过程:
public synchronized boolean enable(IBluetoothDeviceCallback callback) {
checkPermissionBluetoothAdmin();
Log.d(TAG,"start enable! ");
// Airplane mode can prevent Bluetooth radio from being turned on.
if (mIsAirplaneSensitive && isAirplaneModeOn()) {
return false;
}
if (mIsEnabled) {
return false;
}
if (mEnableThread != null && mEnableThread.isAlive()) {
return false;
}
// 主要的启动过程是放在一个新起的线程里面,但是不管能不能启动
// 仍然返回了true
mEnableThread = new EnableThread(callback);
mEnableThread.start();
//
return true;
}
private EnableThread mEnableThread;
private class EnableThread extends Thread {
private final IBluetoothDeviceCallback mEnableCallback;
public EnableThread(IBluetoothDeviceCallback callback) {
mEnableCallback = callback;
}
public void run() {
boolean res = enableNative();
if (res) {
mEventLoop.start();
}
if (mEnableCallback != null) {
try {
// 通过回调函数来表明是否正常启动蓝牙设备
mEnableCallback.onEnableResult(res ?
BluetoothDevice.RESULT_SUCCESS :
BluetoothDevice.RESULT_FAILURE);
} catch (RemoteException e) {}
}
if (res) {
mIsEnabled = true;
mIsDiscovering = false;
Intent intent = new Intent(BluetoothIntent.ENABLED_ACTION);
mContext.sendBroadcast(intent);
}
}else{
mIsEnabled = false;
mIsDiscovering = false;
}
mEnableThread = null;
}
}
// 这个回调函数将被作为参数传进bluetoothservice 里面的enable(IBluetoothDeviceCallback callback)
static class DeviceCallback extends IBluetoothDeviceCallback.Stub {
Handler messageHandler;
public void setHandler(Handler handler) {
synchronized (this) {
messageHandler = handler;
}
public void onEnableResult(int result) {
switch(result) {
// 启动不成功的时候执行
case BluetoothDevice.RESULT_FAILURE:
messageHandler.sendMessage(messageHandler.obtainMessage(EVENT_FAILED_BT_ENABLE,0));
break;
}
}
// 配对完成时执行
public void onCreateBondingResult(String address, int result) {
synchronized (this) {
if (messageHandler != null) {
if (result == BluetoothDevice.RESULT_FAILURE) {
messageHandler.sendMessage(messageHandler.obtainMessage(
HANDLE_PAIRING_FAILED, address));
} else {
messageHandler.sendMessage(messageHandler.obtainMessage(
HANDLE_PAIRING_PASSED, address));
}
}
}
}
};
关闭过程
public synchronized boolean disable() {
checkPermissionBluetoothAdmin();
if (mEnableThread != null && mEnableThread.isAlive()) {
return false;
}
if (!mIsEnabled) {
return true;
}
if(!disableNative()){
Log.d(TAG,"disableNative false ");
return false;
}
mEventLoop.stop();
mIsEnabled = false;
mIsDiscovering = false;
Intent intent = new Intent(BluetoothIntent.DISABLED_ACTION);
mContext.sendBroadcast(intent);
return true;
}
配对过程
private void doPair(Preference pref, String address) {
pref.setEnabled(false);
pref.setSummary(STR_PAIRING);
if (mPinEdit != null){
String strPIN = mPinEdit.getText().toString();
mBluetooth.writePinCode(address, strPIN);
mBluetooth.createBonding(address, sDeviceCallback);
}
}
最后根据配对的结果执行回调函数的onCreateBondingResult。