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张图片

 

 

 

 

 

 

 

 

 

 

 

 

更多相关文章

  1. 在android中创建包含对象数组对象List 的Parcelable
  2. android解析二维数组对象key:value
  3. Android 4.2 BT系统之蓝牙关闭过程全跟踪
  4. Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
  5. Android低功耗蓝牙应用开发获取的服务UUID
  6. 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发详解
  7. [Android M] Bluedroid修改蓝牙默认名称

随机推荐

  1. android 发短信、打电话、发邮件
  2. [Android]在代码里运行另一个程序的方法
  3. Android(安卓)实现Activity后台运行
  4. Android(安卓)代码片段
  5. Android震动与提示音实现代码
  6. 安卓Banner的使用
  7. android-项目结构详解
  8. android SL4A
  9. Android(安卓)添加设置自定义字体
  10. Android(安卓)常用的第三方开源框架(非基