Android蓝牙初级一

Android蓝牙的开启,收索,Socket连接(据说不需要配对就可以连接,测试中…)

一,权限

//使用蓝牙设备权限
<uses-permission android:name="android.permission.BLUETOOTH" />
//蓝牙设备管理员权限(开启蓝牙时可以不显示弹框)
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

二,开启蓝牙
进入代码之前这里先定义一些基本的对象,后面代码会使用

//蓝牙适配器对象,提供开启,关闭蓝牙以及收索设备等相关接口
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//已配对设备列表
private ArrayList<BluetoothDevice> mPairedDevices = new ArrayList<>();
//当前能收索到的设备列表
private ArrayList<BluetoothDevice> mNewDevices = new ArrayList<>();

正式开启蓝牙设备

/**
     * 开启蓝牙设备
     */
    public boolean open() {
        if (!mBluetoothAdapter.isEnabled()) {
            return mBluetoothAdapter.enable();
        } else {
            return true;
        }
    }

三,收索蓝牙
本来开始收索蓝牙只需要下面这行代码就OK了,

mBluetoothAdapter.startDiscovery();

但是在时间的使用中可以添加一些判断,这样效果会更好些。如下面这种写法。

    /**
     * 开始收索蓝牙设备
     * 系统异步方法,收索到的设备信息将会在BroadcastReceiver 中返回
     *
     * 建议放在子线程中运行该代码
     */
    public void startDiscovery() {


        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
        while (!mBluetoothAdapter.startDiscovery()) {
            mNewDevices.clear();
            mBluetoothAdapter.startDiscovery();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

收索的结果系统会通过广播发送出来,所以编写相应的广播接收器来监听收索结果。

BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //找到设备
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {

                Log.v("tag", "find one");
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {

                    Log.v("tag", "find device:" + device.getName() + device.getAddress());
                    mNewDevices.add(device);
                }
            }
            //搜索完成
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                Log.v("tag", "find over :" + mNewDevices.size());
                if (mNewDevices.size() == 0) {
                    Log.v("tag", "find over  but can not find anyone");
                }

            }
        }
    };

    private void register() {
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        mContext.registerReceiver(mReceiver, filter);

        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        mContext.registerReceiver(mReceiver, filter);

    }

使用中要调用register方法进行注册。
四,Socket连接见下节分析

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值