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、时序图

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android framework是Android系统的核心部分,用于提供开发应用程序所需的各种基本功能和服务。学习Android framework是成为一名Android开发者的重要一步。 了解Android framework的最佳方法之一是通过实践和阅读相关的文档。可以开始学习的几个关键主题包括: 1. Android四大组件:了解Activity、Service、Broadcast Receiver和Content Provider的概念、用法和生命周期。 2. UI开发:熟悉Android的UI组件和布局,掌握UI控件的使用和界面的设计。 3. 数据存储:学习如何在Android应用程序中使用SQLite数据库来存储和检索数据。 4. 网络通信:了解如何使用Android提供的API进行网络通信,包括HTTP请求和响应的处理。 5. 多媒体:学习如何处理图片、音频和视频等多媒体资源。 6. 权限和安全性:了解Android的权限模型和安全性要求,并学习如何在应用程序中正确处理权限。 7. 性能优化:学习如何优化应用程序的性能,包括减少内存占用、优化UI响应等方面。 此外,可以参考Google提供的Android开发文档和教程,其中包含了丰富的示例代码和实践指导。还可以参加Android开发者社区的讨论和参与开源项目,与其他开发者交流和分享经验。 总之,学习Android framework需要时间和实践,通过不断的探索和实践,逐渐掌握Android系统的各个方面,从而成为一名优秀的Android开发者。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值