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

Android蓝牙连接汽车OBD设备

2014-04-30 16:28 337 查看
//设备连接
public class BluetoothConnect implements Runnable {

private static final UUID CONNECT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private Loger loger = Loger.getLoger();
private BluetoothDevice mDevice;
private BluetoothSocket mSocket;
private IInterface iInterface;
private OutputStream out;
private boolean isConnected = false;

public BluetoothConnect(BluetoothDevice device) {
this.mDevice = device;
}

public interface IInterface {

void connected(BluetoothDevice device);

void receive(String string);

void disconnect(BluetoothDevice device);

void connectError();
}

@Override
public void run() {
try {
if (iInterface == null) {
loger.e("IBluetoothData interface is null");
return;
}
loger.d("connecting bluetooth device................");
int sdk = Build.VERSION.SDK_INT;
if (sdk >= 10) {
mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(CONNECT_UUID);
} else {
mSocket = mDevice.createRfcommSocketToServiceRecord(CONNECT_UUID);
}
mSocket.connect();
iInterface.connected(mDevice);
isConnected = true;
InputStream in = mSocket.getInputStream();
out = mSocket.getOutputStream();
String s = "";
byte[] buffer = new byte[1024 * 3];
int len;
while((len = in.read(buffer)) > 0){
s += new String(buffer, 0, len, "GBK");
int index = -1;
while ((index = s.indexOf("\r\n")) != -1) {
iInterface.receive(s.substring(0, index + 2));
s = s.substring(index + 2, s.length());
index = -1;
}
}
} catch (IOException e) {
loger.e("", e);
iInterface.connectError();
}finally{
iInterface.disconnect(mDevice);
isConnected = false;
}
}

public void setInterface(IInterface iInterface) {
this.iInterface = iInterface;
}

public boolean isConnected(){
return isConnected;
}

public void write(byte[] buffer) {
if (out != null) {
try {
out.write(buffer);
out.flush();
} catch (IOException e) {
loger.e("write error", e);
}
}
}

public void close() {
try {
if (mSocket != null) {
mSocket.close();
}
} catch (IOException e) {
loger.e("close error", e);
}
}
}


搜索设备

//首先动态注册广播
IntentFilter filter = new IntentFilter();
filter.setPriority(Integer.MAX_VALUE);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(receiver, filter);
//创建广播接收器
public BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device;
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
String address = device.getAddress();
int state = device.getBondState();
loger.d(String.format("found bluetooth device name=%s address=%s state=%s", name, address, state == BluetoothDevice.BOND_BONDED ? "BOND_BONDED" : "BOND_NONE"));
if (!adapter.getDevices().contains(device)) {
adapter.addDevice(device);
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
loger.d("select bluetooth device over!!!");
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (device.getBondState()) {
case BluetoothDevice.BOND_BONDING:
loger.d("bluetooth device bonding....");
break;
case BluetoothDevice.BOND_BONDED:
adapter.notifyDataSetChanged();
loger.d("bluetooth device bonded");
break;
case BluetoothDevice.BOND_NONE:
loger.d("bluetooth device none!!!");
break;
default:
break;
}
}
}
};

//配对方法
public boolean createBond(BluetoothDevice device) {
try {
Method createBondMethod = device.getClass().getMethod("createBond");
return (Boolean) createBondMethod.invoke(device);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: