最近需要做一个从车载电脑里面读取汽车的发动机相关信息的工作就是所谓的OBD信息采集,这些信息 通过经典蓝牙传输到目标设备上。与现在的免握手的蓝牙连接相比还是有很多不同,所以在这里mark一下,希望能帮助有需要的朋友。
扫面蓝牙
首先获得蓝牙权限,然后打开蓝牙,设置蓝牙扫面结果监听然后开启蓝牙扫描
OBDBlueDevice obdBlueDevice=new OBDBlueDevice(this,"此处输入mac地址,如果没有就为空");
obdBlueDevice.NoticeRefreshData(new NoticeRefresh() {
@Override
public void noticeRefresh(ArrayList devices) {
if(devices.size()>0){
if(adapter==null){
progressBar.setVisibility(View.GONE);
//添加蓝牙扫面得到的结果到列表适配器中
adapter=new ListAdapter(devices,sdkActivity.this);
listView.setVisibility(View.VISIBLE);
listView.setAdapter(adapter);
//选中的蓝牙连接
adapter.mOnItemClick(new ListAdapter.OnItemClick() {
@Override
public void onItemClick(BluetoothDevice device) {
obdBlueDevice.setConnectToItem(device);
}
});
}
}else {
progressBar.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"没有发现设备",Toast.LENGTH_SHORT).show();
}
}
});
通过OBDBlueDevice类的方法开启数据的监听,刷新和选中蓝牙对象的连接操作。
下面对封装的OBDBlueDevice的解析
初始化OBDBlueDevice
//初始化device,拿到默认的bluetoothAdapter
public OBDBlueDevice(Context context,String mac) {
this.context=context;
this.mac=mac;
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
然后就可以开始工作了,这里用了Rxjava的方式开启蓝牙扫描
Consumer DeviceObserver=new Consumer() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if(aBoolean){
//开始搜索蓝牙设备
SearchBlueToothDevice();
}
}
};
private Observable DeviceObservable=Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
if(bluetoothAdapter==null){
emitter.onNext(false);
emitter.onComplete();
throw new NoBlueException("该设备不支持蓝牙");
}else {
if(!bluetoothAdapter.isEnabled()){
emitter.onNext(false);
emitter.onComplete();
Toast.makeText(context.getApplicationContext(),"请开启蓝牙",Toast.LENGTH_SHORT).show();
}else {
emitter.onNext(true);
emitter.onComplete();
}
}
}
});
private void SearchBlueToothDevice() {
Log.e(TAG,"开始搜索");
//这里扫描得到的蓝牙结果会通过广播传送
new Thread(new Runnable() {
@Override
public void run() {
if(bluetoothAdapter.isDiscovering()){
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.startDiscovery();
}else {
bluetoothAdapter.startDiscovery();
}
}
}).start();
}
//开启蓝牙扫描的方法
public void starWork() {
//注册蓝牙扫面广播接收器
context.registerReceiver(mreceiver,getIntentFileter());
//查找蓝牙设备
DeviceObservable.subscribe(DeviceObserver);
}
//创建广播Intent
private IntentFilter getIntentFileter(){
IntentFilter intentFilter=new IntentFilter();
//添加广播监听事件
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
return intentFilter;
}
private final BroadcastReceiver mreceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
//发现蓝牙,并从Intent中获取BluetoothDevice对象
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e(TAG,"发现设备"+device.getAddress());
//去重
if(!bluelist.contains(device)){
bluelist.add(device);
}
}else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
//发现设备完成,返回并通知,开始连接
Log.e(TAG,"扫描完成");
if(bluelist.size()>0){
//通知刷新列表
if(noticeRefresh!=null){
noticeRefresh.noticeRefresh(bluelist);
}
//设置扫描完成标志
isFinish=true;
}else {
Toast.makeText(OBDBlueDevice.this.context.getApplicationContext(),"没有发现蓝牙设备",Toast.LENGTH_SHORT).show();
}
}
}
};
下面开始蓝牙的连接,当点击想要连接的蓝牙,会弹出一个系统的对话框,要求输入连接的配对密码,密码正确就可以获取蓝牙的输入输出流了。
//蓝牙连接
public void setConnectToItem(BluetoothDevice itemDevice){
BluetoothClientThread connectThread=new BluetoothClientThread(itemDevice);
new Thread(connectThread).start();
}
class BluetoothClientThread implements Runnable {
public BluetoothClientThread(BluetoothDevice device) {
if(bluetoothAdapter.isEnabled()){
//获得蓝牙Socket
BluetoothSocket tmp=null;
try {
tmp=device.createRfcommSocketToServiceRecord(mUUID);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket=tmp;
}
}
@Override
public void run() {
if(bluetoothAdapter.isDiscovering()){
bluetoothAdapter.cancelDiscovery();
}
if(inputStream==null||outputStream==null){
try {
//连接蓝牙设备,获取输入输出流信息
OBDBlueDevice.this.bluetoothSocket.connect(); OBDBlueDevice.this.inputStream=bluetoothSocket.getInputStream(); OBDBlueDevice.this.outputStream=bluetoothSocket.getOutputStream();
isConnect=true;
Log.e(TAG,"socket连接成功");
} catch (IOException e) {
Log.e(TAG,"socket连接失败");
isConnect=false;
e.printStackTrace();
}
}
//如果蓝牙连接成功,就可以开始传输数据了
if(isConnect()){
//通过流传输你想传输的数据
}
}
}