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、調用關係類圖

 //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、時序圖

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章