您的位置:首页 > 移动开发 > Android开发

android 蓝牙通信(一)

2015-09-23 16:01 344 查看

1、需要在AndroidMainfest.xml里声明蓝牙权限

[code]    <!-- 设置蓝牙的可见时间,以便被其他设备发现并连接 -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <!--检测一个设备是否有蓝牙设备,开启蓝牙设备,获取配对的设备-->
    <uses-permission android:name="android.permission.BLUETOOTH" />


2、BluetoothAdapter

BluetoothAdapter代表了移动设备的本地的蓝牙适配器, 通过该蓝牙适配器可以对蓝牙进行基本操作,例如:

启动设备发现(startDiscovery),

获取已配对设备(getBoundedDevices),

通过mac蓝牙地址获取蓝牙设备(getRemoteDevice),

从其它设备创建一个监听连接(listenUsingRfcommWithServiceRecord);

2.1 获取蓝牙适配器对象

[code] BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();


[code]    //判断是否支持蓝牙设备
    private void isSupportBlueTooth() {
        if(mAdapter == null){
            Log.i(TAG, "本机没有蓝牙设备");
        }else{
            Log.i(TAG, "本机有蓝牙设备");
        }
    }


[code]    /**
     * 判断蓝牙状态,不可用则打开蓝牙,可用则输出一些蓝牙信息
     */
    private void judgeState() {
        if(!mAdapter.isEnabled()){
            Log.i(TAG, "蓝牙设备目前不可用");
            mAdapter.enable();//打开本地蓝牙适配器
        }else{
            Log.i(TAG, "蓝牙设备目前可用,下面获取蓝牙设备信息");
            Log.d(TAG, "蓝牙地址:"+mAdapter.getAddress());
            Log.d(TAG, "蓝牙名称:"+mAdapter.getName());
            Log.d(TAG, "蓝牙状态方法(是否可用):"+mAdapter.getState());
            Log.d(TAG, "检测蓝牙地址是否正确: 1、地址aaa:"+mAdapter.checkBluetoothAddress("aaa")+
                    "   2、 地址00:43:A8:23:10:F0 :"+mAdapter.checkBluetoothAddress("00:43:A8:23:10:F0"));
        }
    }


2.2 搜索周围蓝牙设备

2.2.1开始搜索只要一行代码

[code]mAdapter.startDiscovery();


2.2.2 通过注册BroadCastReciver,获取蓝牙信息

首先,定义广播接收者

[code]    // case后面必须跟常量,必须要常量 加final
    static final int ACTION_FOND = 11011;
    static final int DISCOVERY_FINISHED = 11012;
    static final int DISCOVERY_START = 11015;

mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();

                    Message msg = new Message();
                    msg.obj = intent;
                    if (BluetoothDevice.ACTION_FOUND.equals(action)) {// 获得已经搜索到的蓝牙设备
                        Log.e(TAG, "接收到action=BluetoothDevice.ACTION_FOUND");
                        msg.what = ACTION_FOND;
                        handler.sendMessage(msg);
                    } 
                else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                            .equals(action)) {// 搜索完毕
                        Log.e(TAG, "接收到 action=BluetoothAdapter.ACTION_DISCOVERY_FINISHED");
                        msg.what = DISCOVERY_FINISHED;
                        handler.sendMessage(msg);
                    }
                else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
                        Log.e(TAG, "接收到 action=BluetoothAdapter.ACTION_DISCOVERY_STARTED");
                        msg.what = DISCOVERY_START;
                        handler.sendMessage(msg);
                    }
                }
            };


动态注册广播地址

[code]IntentFilter btDiscoveryFilter = new IntentFilter();
            btDiscoveryFilter
                    .addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);// 开始搜索
            btDiscoveryFilter
                    .addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);// 搜索完毕
            btDiscoveryFilter.addAction(BluetoothDevice.ACTION_FOUND);// 发现远程设备

            this.registerReceiver(mReceiver, btDiscoveryFilter);


实现Handler

[code]handler = new Handler(new Handler.Callback() {
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                case DISCOVERY_START:
                    Log.d(TAG, "-----------搜索开始-------------");
                    break;
                case ACTION_FOND:
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device = intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Log.i(TAG, "find device:" + device.getName());
                    break;
                case DISCOVERY_FINISHED:
                    Log.d(TAG, "-----------搜索完成-------------");
                    break;
                }
                return false;
            }
        });


2.3 效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: