您的位置:首页 > 其它

浅谈安卓蓝牙开发

2015-06-16 22:38 218 查看
PS:只是记录和说明在做蓝牙开发过程中遇到一些问题和想法,说错请谅解

关于蓝牙开发,如果不是要实现自动配对和自动连接的话,需要以下步骤

1 . 打开蓝牙适配器

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(MainActivity.this, "空值", Toast.LENGTH_SHORT).show();
}
if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable(); //强制性打开
}


2 . 注册广播,搜索蓝牙

/* 1. 编写一个继承广播的类 */
public class BlueToothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ( BluetoothDevice.ACTION_FOUND.equals(action) ){
bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if( !deviceList.contains(bluetoothDevice) ){
/*private List<BluetoothDevice > deviceList;*/
deviceList.add(bluetoothDevice);
/*private List<String > devices;*/
devices.add(bluetoothDevice.getName() + bluetoothDevice.getAddress() + "\n" +
bluetoothDevice.getBondState());
}
} else if ( BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action) ){
//这里showDevices函数是用于显示在listview中
showDevices();
return ;
}
}
}

/* 2. 注册广播*/
blueToothReceiver = new BlueToothReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FI
NISHED);
this.registerReceiver(blueToothReceiver, intentFilter);


3 . 搜索蓝牙

Intent enable = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATIO
N, 1200);
startActivity(enable);
bluetoothAdapter.startDiscovery(); //开始搜索蓝牙,通过广播的形式
/*当然可以不通过广播的形式去搜索周边的蓝牙,可以通过获取手机已经绑定的周边蓝牙*/
/*只要实现 BluetoothAdapter.getDefaultAdapter().getBondedDevices();
将获取到手机绑定的所有的蓝牙。
*/


4 . 蓝牙设备连接

/*一些事项说明:
* 1. 在一开始做蓝牙开发的时候,我是利用两部手机进行蓝牙通讯的。根据官
* 方的API说明,蓝牙通讯是需要相同的UUID才能进行连接的。当时用一个SPP
* 的UUID进行连接,发现两部手机的蓝牙无法进行通讯。在想根据
* SerialPortServiceClass_UUID = '{00001101-0000-1000-8000-
* 00805F9B34FB}'这个单词猜测,该UUID估计只能是实现蓝牙模块用串口跟
* 手机进行连接,拿到蓝牙模块后发现真的是直接连接上去了。。。
*
* 2. 至于网上其他方法,例如反射机制方法去连接,也是可以的。
*
* 3. 遇到抛出异常----service discovery failed,不知道是不是一开始
* 没有正常关闭BluetoothSocket的原因,导致抛出这个异常。
*
* 4. connection refused。在测试时候发现,由于蓝牙适配器有时候是没
* 正常打开的,导致异常的出现。
*
* 5. 在进行连接蓝牙的时候,经常抛出这两个异常。网上有人说由于安卓系统
* 存在的bug,估计也不知道是不是。发现只有正确的打开蓝牙适配器,正确的
* 连接蓝牙,在退出程序时候,将所有打开的接口全部关闭,才不会发生抛出异
* 常的情况。(自己的想法,错误勿喷。)
*/

bluetoothAdapter.cancelDiscovery();
UUID my_uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
try {
bluetoothSocket =
device.createRfcommSocketToServiceRecord(my_uuid);
bluetoothSocket.connect();
} catch (IOException e) {
e.printStackTrace();
}


5 . 打开 I/O 流

try {
InputStream inputStream = bluetoothSocket.getInputStream();
OutputStram outputStream = bluetoothSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}


6 . 关于要实现自动配对和自动连接的,利用反射机制的方法。

可以查看下面这篇文章

蓝牙自动配对和连接

关于里面所说到利用反射机制得到的函数方法,在官方的SDK版本中有一些函数并没有出现。例如cancelPairingUserInput这个方法,查看几个最新的SDK版本都没有这个函数,所以也不知道到底是怎么实现的。当然可以试试看滴。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: