Android 蓝牙 send/receive 哈希图
Android bluetooth send/receive a hashmap
我正在尝试通过蓝牙将哈希图从一台设备发送到另一台设备。我正在这样创建哈希图,然后调用连接线程 writeObject
方法:
Map<String,String> subInfo = new HashMap<>();
subInfo.put("type", "subInfo");
subInfo.put("num", "1");
BTService.connectedThread.writeObject(subInfo);
我的蓝牙服务有以下 class 用于在后台线程中处理设备之间的数据传输:
public static class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private final ObjectInputStream mmObjInStream;
private final ObjectOutputStream mmObjOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
ObjectInputStream tmpObjIn = null;
ObjectOutputStream tmpObjOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
tmpObjIn = new ObjectInputStream(socket.getInputStream());
tmpObjOut = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
mmObjInStream = tmpObjIn;
mmObjOutStream = tmpObjOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (!this.isInterrupted()) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
e.printStackTrace();
break;
}
try {
//Read objects from ObjectInputStream
Object object = mmObjInStream.readObject();
// Send the obtained object to the UI activity
mHandler.obtainMessage(Constants.MESSAGE_READ_OBJECT, -1, 0, object)
.sendToTarget();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeObject(Object object) {
try {
mmObjOutStream.writeObject(object);
}catch(Exception e){
Log.e(TAG, "Error ObjectOutputStream: " + e.getLocalizedMessage());
}
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
this.interrupt();
} catch (IOException e) {
e.printStackTrace();
}
}
}
所以当我调用 writeObject
方法时,它应该将对象发送到上面线程的对象输出流中。
在其他设备上,代码类似。它只是在对象输入流上侦听对象,一旦找到它,它就会向主 activity:
中的消息处理程序发送一条 MESSAGE_READ_OBJECT
消息
static class MessageHandler extends Handler {
private final WeakReference<MainActivity> mActivity;
MessageHandler(MainActivity activity) {
mActivity = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg)
{
final MainActivity mainActivity = mActivity.get();
if (mainActivity == null) {
return;
}
switch(msg.what){
case Constants.MESSAGE_READ_OBJECT:
Object receivedObject = msg.obj;
//How should I pull out the hashmap here?
switch(type){
case "subInfo":
//do some things
break;
}
break;
}
}
}
在我的消息处理程序中重新组合散列图的正确方法是什么?我需要检查地图的 "type" 键并根据它进行切换,但截至目前我不确定如何取回地图对象。我的消息处理程序中的 receivedObject
对象的行为似乎不像哈希图,并且没有用于提取键和值的典型方法
此外,与其通过蓝牙发送通用的 Object
,不如直接发送 Map
类型的对象更有意义?
直接转换即可
HashMap mHashmap = (HashMap) obj;
用法:https://coding.net/u/zhenbeiju/p/javanlp_/git/blob/master/src/com/voice/common/util/nlp/Main.java
我正在尝试通过蓝牙将哈希图从一台设备发送到另一台设备。我正在这样创建哈希图,然后调用连接线程 writeObject
方法:
Map<String,String> subInfo = new HashMap<>();
subInfo.put("type", "subInfo");
subInfo.put("num", "1");
BTService.connectedThread.writeObject(subInfo);
我的蓝牙服务有以下 class 用于在后台线程中处理设备之间的数据传输:
public static class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private final ObjectInputStream mmObjInStream;
private final ObjectOutputStream mmObjOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
ObjectInputStream tmpObjIn = null;
ObjectOutputStream tmpObjOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
tmpObjIn = new ObjectInputStream(socket.getInputStream());
tmpObjOut = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
mmObjInStream = tmpObjIn;
mmObjOutStream = tmpObjOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (!this.isInterrupted()) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
e.printStackTrace();
break;
}
try {
//Read objects from ObjectInputStream
Object object = mmObjInStream.readObject();
// Send the obtained object to the UI activity
mHandler.obtainMessage(Constants.MESSAGE_READ_OBJECT, -1, 0, object)
.sendToTarget();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeObject(Object object) {
try {
mmObjOutStream.writeObject(object);
}catch(Exception e){
Log.e(TAG, "Error ObjectOutputStream: " + e.getLocalizedMessage());
}
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
this.interrupt();
} catch (IOException e) {
e.printStackTrace();
}
}
}
所以当我调用 writeObject
方法时,它应该将对象发送到上面线程的对象输出流中。
在其他设备上,代码类似。它只是在对象输入流上侦听对象,一旦找到它,它就会向主 activity:
中的消息处理程序发送一条MESSAGE_READ_OBJECT
消息
static class MessageHandler extends Handler {
private final WeakReference<MainActivity> mActivity;
MessageHandler(MainActivity activity) {
mActivity = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg)
{
final MainActivity mainActivity = mActivity.get();
if (mainActivity == null) {
return;
}
switch(msg.what){
case Constants.MESSAGE_READ_OBJECT:
Object receivedObject = msg.obj;
//How should I pull out the hashmap here?
switch(type){
case "subInfo":
//do some things
break;
}
break;
}
}
}
在我的消息处理程序中重新组合散列图的正确方法是什么?我需要检查地图的 "type" 键并根据它进行切换,但截至目前我不确定如何取回地图对象。我的消息处理程序中的 receivedObject
对象的行为似乎不像哈希图,并且没有用于提取键和值的典型方法
此外,与其通过蓝牙发送通用的 Object
,不如直接发送 Map
类型的对象更有意义?
直接转换即可
HashMap mHashmap = (HashMap) obj;
用法:https://coding.net/u/zhenbeiju/p/javanlp_/git/blob/master/src/com/voice/common/util/nlp/Main.java