使用安卓完成蓝牙聊天
项目地址
最终效果
实现
填写两个layout文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/background">
<LinearLayout
android:id="@+id/layout"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18sp"
android:text="@string/pairedDevice"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textAlignment="center"
android:textColor="@color/white"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@string/foundDevice"/>
</LinearLayout>
<LinearLayout
android:layout_below="@id/layout"
android:layout_above="@id/button_scan"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/paired_devices"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</ListView>
<ListView
android:id="@+id/new_devices"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<Button
android:id="@+id/button_scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/bord"
android:text="@string/scan" />
<ProgressBar
android:id="@+id/processbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/background"
tools:context="android.myapplication.MainActivity">
<ListView
android:id="@+id/list_conversation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/re_layout">
</ListView>
<RelativeLayout
android:id="@+id/re_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent">
<EditText
android:id="@+id/edit_text_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/edit_hint"
android:inputType="text" />
<Button
android:id="@+id/button_send"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:background="@mipmap/enter_48px" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
编写业务代码
ChatService
内容较多,仅列出部分
private class ConnectThread extends Thread{
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device){
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
mmDevice=device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try{
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
}catch (IOException e){}
mmSocket = tmp;
}
public void run(){
// Cancel discovery because it will slow down the connection
mAdapter.cancelDiscovery();
try{
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
}catch (IOException e){
connectionFailed();
// Unable to connect; close the socket and get out
try{
mmSocket.close();
}catch (IOException e2){}
//ChatService.this.start();
return;
}
synchronized(ChatService.this){
mConnectedThread = null;
}
connected(mmSocket,mmDevice);
}
public void cancel(){
/* try{
mmSocket.close();
}catch (IOException e){}*/
}
}
上述方法用于 连接线程,专门用来对外发出连接对方蓝牙的请求并进行处理
在MainActivity中编写Handler
private final Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what){
case MESSAGE_STATE_CHANGE:
switch (msg.arg1){
case ChatService.STATE_CONNECTED:
mConversationArrayAdapter.clear();
break;
case ChatService.STATE_CONNECTING:
break;
case ChatService.STATE_LISTEN:
case ChatService.STATE_NONE:
break;
}break;
case MESSAGE_WRITE:
byte[]writeBuf =(byte[])msg.obj;
String writeMessage=new String(writeBuf);
mConversationArrayAdapter.add("我: " + writeMessage);
break;
case MESSAGE_READ:
byte[]readBuf =(byte[])msg.obj;
String readMessage=new String(readBuf,0,msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName+": "
+readMessage);
break;
case MESSAGE_DEVICE_NAME:
mConnectedDeviceName=msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(),
"链接到"+mConnectedDeviceName,Toast.LENGTH_SHORT)
.show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(),
msg.getData().getString(TOAST),Toast.LENGTH_SHORT)
.show();
break;
}
}
};
心得体会
蓝牙通信我感觉难度较大,比之前的地图要大很多。
自己也没有静下心来认真了解,希望最后的课程设计能够沉下心来,认真完成,学习有价值的内容