gpt4 book ai didi

android - 在外部 android 应用程序中读取蓝牙消息

转载 作者:行者123 更新时间:2023-11-29 20:15:01 25 4
gpt4 key购买 nike

我是 android 蓝牙的新手,我想使用内部存储或 sqlite 读取蓝牙消息并将其存储在外部 android 应用程序(我的)中。我试过 GitHub 上的 android bluetooth-chat 示例,但我不知道如何实现我的想法。

任何帮助都会有帮助,谢谢

最佳答案

蓝牙消息的交换包含在 API 的 android.bluetooth 部分。

http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection

这是管理连接和发送/接收消息的基本示例:

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

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 (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}

/* 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) { }
}

/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}

关于android - 在外部 android 应用程序中读取蓝牙消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34081383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com