栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

蓝牙bluetooth的framework层学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

蓝牙bluetooth的framework层学习

参考:Android -- Bluetooth framework启动过程简析_第一序列丶的博客-CSDN博客

framework层包括 api、service、bluetooth.apk,这里大致记录下它们间的联系

1、mBluetooth和mBluetoothBinder生成的

case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: {
	IBinder service = (IBinder) msg.obj;
	
	mBinding = false;
	mBluetoothBinder = service;
	mBluetooth = IBluetooth.Stub.asInterface(Binder.allowBlocking(service));

2、BluetoothAdapter与BluetoothManagerService.java建立联系,注册回调

BluetoothAdapter.java

system/bt/binder/android/bluetooth/IBluetoothManager.aidl

private IBluetooth mService;  这个是链接bluetooth.apk的service

BluetoothAdapter(IBluetoothManager managerService) {
	mService = managerService.registerAdapter(mManagerCallback); //这儿返回的mService有可能是空
}

private final IBluetoothManagerCallback mManagerCallback =
		new IBluetoothManagerCallback.Stub() {
			public void onBluetoothServiceUp(IBluetooth bluetoothService) {
				mService = bluetoothService; 
			}
		}

3、各个模块监听BluetoothState的状态
在BluetoothManagerService.java
回调注册:

public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
	Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK);
	msg.obj = callback;
	mHandler.sendMessage(msg);
}

case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: {
	IBluetoothStateChangeCallback callback =
			(IBluetoothStateChangeCallback) msg.obj;
	mStateChangeCallbacks.register(callback);
	break;
}

回调调用:

    private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() {
        public void onBluetoothStateChange(int prevState, int newState) throws RemoteException {
            Message msg =
                    mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE, prevState, newState);
            mHandler.sendMessage(msg);
        }
    };

case MESSAGE_BLUETOOTH_STATE_CHANGE: {
	int prevState = msg.arg1;
	int newState = msg.arg2;
	mState = newState;
	bluetoothStateChangeHandler(prevState, newState);

private void sendBluetoothStateCallback(boolean isUp) {
		int n = mStateChangeCallbacks.beginBroadcast();
		for (int i = 0; i < n; i++) {
			try {
				mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
			} catch (RemoteException e) {
				Slog.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i, e);
			}
		}
}

各个模块注册监听bluetooth状态
比如:BluetoothProfileConnector.java

void connect(Context context, BluetoothProfile.ServiceListener listener) {
	IBluetoothManager mgr = BluetoothAdapter.getDefaultAdapter().getBluetoothManager();
	 mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
	doBind();
}

4、使能蓝牙开关
BluetoothAdapter.java
enable() , disable(),enableBLE() 可以使能蓝牙,调用到bluetoothmanagerservice.java

BluetoothManagerService.java
public boolean enable(String packageName) throws RemoteException {
	sendEnableMsg(false,
			BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
}

case MESSAGE_ENABLE:
	handleEnable(mQuietEnable);

5、bluetoothmanagerservice.java与bluetooth.apk建立联系

private class BluetoothServiceConnection implements ServiceConnection {
	private BluetoothServiceConnection() {
	}

	public void onServiceConnected(ComponentName componentName, IBinder service) {
		String name = componentName.getClassName();
		if (name.equals("com.android.bluetooth.btservice.AdapterService")) {
			msg.arg1 = 1;
		} else {
			if (!name.equals("com.android.bluetooth.gatt.GattService")) {
				Slog.e("wqy--BluetoothManagerService", "Unknown service connected: " + name);
			}
		}
	}

	public void onServiceDisconnected(ComponentName componentName) {
	}
}

private BluetoothManagerService.BluetoothServiceConnection mConnection = new BluetoothManagerService.BluetoothServiceConnection();

private void handleEnable(boolean quietMode) {
	if (!doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
			UserHandle.CURRENT)) {}
}

boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
	Slog.e(TAG, "doBind....");
	ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
	intent.setComponent(comp);
	if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
		Slog.e(TAG, "Fail to bind to: " + intent);
		return false;
	}
	return true;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1041172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号