手機客戶端的實現
通過手機自身所帶的藍牙實現連接藍牙模塊,從而實現通過單片機控制繼電器,實現弱電控制強電(由之前項目拆分出來用來演示的小demo)
首先要實現開啓藍牙設備,以及設置可見時間
{
BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
if(!adapter.isEnabled())
{
adapter.enable();
}
Intent enable new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,3600);
startActivity(enable);
Intent searchIntent =new Intent(thia,led.class);
startActivity(searchIntent);
}
獲得一個BluetoothAdapter適配器,通過getDefaultAdapter()獲得系統默認藍牙適配器。
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static BluetoothAdapter myAdapter=null;
private ConnectThread myConnectThread;
private static Handler myHandler;
private ConnectedThread myConnectedThread;
private static Bluetoothserive mBluetoothserive = new Bluetoothserive();
//private static int mState;
// Constants that indicate the current connection state
public static final int STATE_NONE = 0; // we're doing nothing
public static final int STATE_LISTEN = 1; // now listening for incoming connections
public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
public static final int STATE_CONNECTED = 3; // now connected to a remote device
private static int mState = STATE_NONE;<pre name="code" class="java">public synchronized void connected(BluetoothSocket socket, BluetoothDevice device)
{
if(myConnectedThread!=null)
{
myConnectedThread.cancel();
myConnectedThread=null;
}
myConnectedThread = new ConnectedThread(socket);
myConnectedThread.start();
setState(STATE_CONNECTED);
Message msg = myHandler.obtainMessage(Kongtiao.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(Kongtiao.TOAST, "連接智能盒成功");
MainActivity.isConnected = true;
msg.setData(bundle);
myHandler.sendMessage(msg);
}
實現讀寫函數
public void write(byte[] out)
{
ConnectedThread r;
synchronized (this)
{
r = myConnectedThread;
}
r.write(out);
}
public void read(byte[] in)
{
ConnectedThread m;
synchronized (this)
{
m = myConnectedThread;
}
}
Connected類,用於流的處理
private class ConnectedThread extends Thread
{
private final BluetoothSocket mySocket;
private final InputStream myInStream;
private final OutputStream myOutStream;
public ConnectedThread(BluetoothSocket socket) {
// 這個是之前的用於連接的socket
mySocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
//從連接的socket裏獲取InputStream和OutputStream
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
myInStream = tmpIn;
myOutStream = tmpOut;
}
public void run()
{
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// 已經連接上以後持續從通道中監聽輸入流的情況
while (true)
{
try {
// 從通道的輸入流InputStream中讀取數據到buffer數組中
bytes = myInStream.read(buffer);
// myInStream.read(bytes);
// 將獲取到數據的消息發送到UI界面,同時也把內容buffer發過去顯示
myHandler.obtainMessage(Kongtiao.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
public void write(byte[] bytes) {
try {
//將buffer內容寫進通道
myOutStream.write(bytes);
// 用於將自己發送給對方的內容也在UI界面顯示
// mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
} catch (IOException e) { }
}
/* //關閉socket,即關閉通道 */
public void cancel() {
try {
mySocket.close();
} catch (IOException e) { }
}
}
接收以及發送字符的形式,十六進制
// byte write[] = new byte[]{-1,0,0x0f};設置多個十六進制字符,可以有效的防止信號干擾
// 十六進制數據聲明
byte write[] = new byte[] { 0x0f };
數據處理:
public final Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(),
msg.getData().getString(TOAST), Toast.LENGTH_SHORT)
.show();
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
//在讀別人發過來的數據的時候,由於別人發過來的是一個byte數組,
//然後數組裏面不是每個元素都是有效數據,所以要自己對數據進行String再構造處理
//第一個參數是字節數組,第二個爲偏移量,(內容是從第一個位置寫入的),第三個參數是長度。
break;
} catch (Exception e) {
// TODO: handle exception
}
default:
break;
}
super.handleMessage(msg);
}
};
Click函數對應的處理
public void onClick(View v) {
switch (v.getId()) {
// 關閉燈
case R.id.guan:
write[0] = 0x02;
break;
// 打開燈
case R.id.dakaikongtiao:
write[0] = 0x01;
break;
default:
break;
}
如果希望獲取源碼或畢設指導,軟、硬件都可以提供協助支持。請打賞支持! 微信掃一掃支付