本文基于《Android创建Service后台常驻服务并使用Broadcast通信》扩展蓝牙通信的功能,使用手机作为Client,HC06芯片作Service进行通信。
void setup()
{
Serial.begin(9600);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
Serial.println(F("L298N Program start..."));
}
void loop()
{
int value = 0;
if ( Serial.available() )
{
value = Serial.read();
}
else
{
value = 'x';
}
if ( value == '0' )
{
go_stop();
Serial.println(F("0:stop"));
}
else if ( value == '1' )
{
go_up();
Serial.println(F("1:up"));
}
else if ( value == '2' )
{
go_down();
Serial.println(F("2:down"));
}
else if ( value == '3' )
{
go_left();
Serial.println(F("3:left"));
}
else if ( value == '4' )
{
go_right();
Serial.println(F("4:right"));
}
delay(30);
}
private static final UUID HC_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothAdapter mBtAdapter = null;
private BluetoothSocket mBtSocket = null;
private OutputStream outStream = null;
private InputStream inStream = null;
private boolean mBtFlag = true;
获取蓝牙适配器方法,由于寻找设备连接设备为耗时动作,放在线程中处理
/**
* Called by onStartCommand, initialize and start runtime thread
*/
private void myStartService() {
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if ( mBtAdapter == null ) {
showToast("Bluetooth unused.");
mBtFlag = false;
return;
}
if ( !mBtAdapter.isEnabled() ) {
mBtFlag = false;
myStopService();
showToast("Open bluetoooth then restart program!!");
return;
}
showToast("Start searching!!");
threadFlag = true;
mThread = new MyThread();
mThread.start();
}
子线程主要做两件事:连接蓝牙设备和读蓝牙信息
/**
* Thread runtime
*/
public class MyThread extends Thread {
@Override
public void run() {
super.run();
myBtConnect();
while( threadFlag ) {
readSerial();
try{
Thread.sleep(30);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
看看蓝牙的连接,其中BluetoothDevice 的获取有两种方法,一种是已知HC06的MAC地址直接连接,文中使用的另一种方式:通过查找已匹配的设备来获取BluetoothDevice。
/**
* device control
*/
public void myBtConnect() {
showToast("Connecting...");
/* Discovery device */
// BluetoothDevice mBtDevice = mBtAdapter.getRemoteDevice(HC_MAC);
BluetoothDevice mBtDevice = null;
Set mBtDevices = mBtAdapter.getBondedDevices();
if ( mBtDevices.size() > 0 ) {
for ( Iterator iterator = mBtDevices.iterator();
iterator.hasNext(); ) {
mBtDevice = (BluetoothDevice)iterator.next();
showToast(mBtDevice.getName() + "|" + mBtDevice.getAddress());
}
}
try {
mBtSocket = mBtDevice.createRfcommSocketToServiceRecord(HC_UUID);
} catch (IOException e) {
e.printStackTrace();
mBtFlag = false;
showToast("Create bluetooth socket error");
}
mBtAdapter.cancelDiscovery();
/* Setup connection */
try {
mBtSocket.connect();
showToast("Connect bluetooth success");
Log.i(TAG, "Connect " + HC_MAC + " Success!");
} catch (IOException e) {
e.printStackTrace();
try {
showToast("Connect error, close");
mBtSocket.close();
mBtFlag = false;
} catch (IOException e1) {
e1.printStackTrace();
}
}
/* I/O initialize */
if ( mBtFlag ) {
try {
inStream = mBtSocket.getInputStream();
outStream = mBtSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
showToast("Bluetooth is ready!");
}
/**
* Read serial data from HC06
*/
public int readSerial() {
int ret = 0;
byte[] rsp = null;
if ( !mBtFlag ) {
return -1;
}
try {
rsp = new byte[inStream.available()];
ret = inStream.read(rsp);
showToast(new String(rsp));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
/**
* Write serial data to HC06
* @param value - command
*/
public void writeSerial(int value) {
String ha = "" + value;
try {
outStream.write(ha.getBytes());
outStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}