ANDROID 蓝牙编程 用 BluetoothAdapter类,你能够在Android设备上查找周边的蓝牙设备然后配对(绑定),蓝牙通讯是基于唯一地址MAC 来相互 传输的,考虑到安全问题Bluetooth 通讯时需要先配对。然后开始相互连接,连接后设备将会共享同一个RFCOMM 通道以便相互传输数据,目前这些实 现在Android 2.0 或更高版本SDK上实现。 一、查找发现 findding/discovering devices 对于Android 查找发现蓝牙设备使用BluetoothAdapter 类的startDiscovery()方法就可以执行一个异步方式获取周边的蓝 牙设备,因为是一个异步的方法所以我们不需要考虑线程被阻塞问题,整个过程大约需要12 秒时间,这时我们紧接着注册一个 BroadcastReceiver 对象来接收查找到的蓝牙设备信息,我们过滤ACTION_FOUND 这个 Intent 动作来获取每个远程设备的详细信息,通过附加参数在Intent 字段EXTRA_DEVICE 和 EXTRA_CLASS, 中包含了每个BluetoothDevice 对象和对象的该设备类型 BluetoothClass ,示例代码 private final BroadcastReceiver cwjReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); myArrayAdapter.add(device.getName() + " android123 " + device.getAddress()); //获取设备名称和mac 地址 } } }; // 注册这个 BroadcastReceiver IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(cwjReceiver, filter); 最后android123 提醒大家需要注意的是,记住在Service 或 Activity 中重写onDestory()方法,使用unregisterReceiver 方法反注册这个BroadcastReceiver 对象保证资源被正确回收。 一些其他的状态变化有 ACTION_SCAN_MODE_CHANGED 额外参数 EXTRA_SCAN_MODE 和 EXTRA_PREVIOUS_SCAN_MODE 以及SCAN_MODE_CONNECTABLE_DISCOVERABLE、 SCAN_MODE_CONNECTABLE 和SCAN_MODE_NONE, 蓝牙模块 二、配对绑定 bnded/paired device 在 Android 中配对一个蓝牙设备可以调用BluetoothAdapter 类的getBondedDevices()方法可以获取已经配对的设备,该方法将会返回一个BluetoothDevice 数组来区分每个...