Android App怎样调用 Frameworks Bluetooth接口

1、Android App开发蓝牙功能demo

1.1 打开蓝牙

public class TestFragment extends Fragment implements View.OnClickListener{
    private static String TAG = GapTestFragment.class.getSimpleName();

    private MainActivity mActivity;

    private BluetoothAdapter mAdapter;

//监听广播

   private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "onReceive action " + action);

            if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
                mScanBtn.setText(R.string.bt_stop_discovrty);
            }

            if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                mScanBtn.setText(R.string.bt_discovrty);
            }

   ../

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //获得操作蓝牙开关等功能的对象mAdapter

     mAdapter = BluetoothAdapter.getDefaultAdapter();

       //注册广播       

       IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
        mActivity.registerReceiver(mReceiver, filter);
    }

   mAdapter.enable();//打开蓝牙

1.2 获取BluetoothA2dpSink对象发起连接

public class A2dpTestFragment extends Fragment implements View.OnClickListener {
    private BluetoothA2dpSink mA2dpService;
    private BluetoothAdapter mAdapter;

    //监听服务连接消息,当连接成功时将proxy赋值到mA2dpService
    private BluetoothProfile.ServiceListener mA2dpServiceListener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (profile == BluetoothProfile.A2DP_SINK) {
                mA2dpService = (BluetoothA2dpSink) proxy;
            }
        @Override
        public void onServiceDisconnected(int profile) {
            if (profile == BluetoothProfile.A2DP_SINK) {
                mA2dpService = null;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_avrcp2, null);

        if (mDevice == null) {
            Toast.makeText(mActivity, "Please select a device", Toast.LENGTH_SHORT).show();
            return null;
        } else if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
            Toast.makeText(mActivity, "Bluetooth is not enabled", Toast.LENGTH_SHORT).show();
            return null;
        }

       //获取mAdapter对象,然后通过mAdapter获取BluetoothA2dpSink对象,服务绑定成功后会通知mA2dpServiceListener
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAdapter.getProfileProxy(mActivity, mA2dpServiceListener, BluetoothProfile.A2DP_SINK);

2、调用关系类图

Android App怎样调用 Frameworks Bluetooth接口_第1张图片

 //getProfileProxy时,根据profileId创建不同的profile对象

   public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener,
            int profile) {
        if (context == null || listener == null) {
            return false;
        }

        if (profile == BluetoothProfile.HEADSET) {
            BluetoothHeadset headset = new BluetoothHeadset(context, listener);
            return true;
        } else if (profile == BluetoothProfile.A2DP) {
            BluetoothA2dp a2dp = new BluetoothA2dp(context, listener);
            return true;
        } else if (profile == BluetoothProfile.A2DP_SINK) {
            BluetoothA2dpSink a2dpSink = new BluetoothA2dpSink(context, listener);

//BluetoothA2dpSink构造函数中,调用doBind绑定A2dpSinkService

BluetoothA2dpSink(Context context, ServiceListener l) {
        mContext = context;
        mServiceListener = l;
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        IBluetoothManager mgr = mAdapter.getBluetoothManager();
        if (mgr != null) {
            try {
                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (RemoteException e) {
                Log.e(TAG, "", e);
            }
        }

        doBind();
    }

    boolean doBind() {
        Intent intent = new Intent(IBluetoothA2dpSink.class.getName());
        ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
        intent.setComponent(comp);
        if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
                mContext.getUser()))
{
            Log.e(TAG, "Could not bind to Bluetooth A2DP Service with " + intent);
            return false;
        }
        return true;
    }

//绑定成功后通知给到app (mServiceListener)

private final ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            if (DBG) Log.d(TAG, "Proxy object connected");
            mService = IBluetoothA2dpSink.Stub.asInterface(Binder.allowBlocking(service));
            if (mServiceListener != null) {
                mServiceListener.onServiceConnected(BluetoothProfile.A2DP_SINK,
                        BluetoothA2dpSink.this);

            }
        }

        public void onServiceDisconnected(ComponentName className) {
            if (DBG) Log.d(TAG, "Proxy object disconnected");
            mService = null;
            if (mServiceListener != null) {
                mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP_SINK);
            }
        }
    };

 

3、时序图

Android App怎样调用 Frameworks Bluetooth接口_第2张图片

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Android-BT学习)