Android:蓝牙连接、套接字和线程

发布于 2024-08-29 16:20:04 字数 702 浏览 3 评论 0原文

我目前正在开发一个 Android 项目,对于 Android 平台来说是个新手。这是我从一篇文章中得到的一个练习,其中我必须创建一个类似 twitter 的应用程序,以便一个 Android 平台可以写入另一个平台,并且它会自动更新。我正在使用 Android 蓝牙模拟器进行测试。

但在开始之前,我在启动蓝牙连接时遇到了一些困难,并尝试了几个教程,包括官方 Android API/示例。

我有一个名为“BlueTweetService”的类,其中有三个方法:configureBluetoothServerSocketconnectToServerDeviceshutdownBluetoothServerSocket

在第一种方法 configureBluetoothServerSocket 中,我必须打开一个 BluetoothServerSocket 并使用属性 String BluetoothTweetName 和 UUID BlueTweetUuid >。

我尝试创建/打开服务器套接字,但无论我做什么,都会出现错误。

如果有人可以帮助我编写如何创建/打开该服务器套接字的代码,我将非常感激。

如果我需要提供一些信息,请告诉我,我会发布它们。

真挚地 梅斯蒂卡

I’m currently working on a Android project and is very newbie to the Android platform. It’s an exercise I got from an article where I have to create a twitterlike application so that one Android platform can write to another and it automatically is updating. I’m using a Bluetooth emulator for Android to test.

But before I start with that I’ve facing some difficulties with the initiating a Bluetooth connection and have tried out several tutorials inclusive the official Android API / samples.

I got a class called “BlueTweetService” and within this I got three methods: configureBluetoothServerSocket, connectToServerDevice and shutdownBluetoothServerSocket.

In the first method, configureBluetoothServerSocket, I’ve to open a BluetoothServerSocket and using the properties String BluetoothTweetName and UUID BlueTweetUuid.

I’ve tried to create / open the serversocket but I gives me errors no matter what I do.

I would really appreciate if someone could help me with the code how to create / open that serversocket.

If I need to provide some information, please let me know and I’ll post them.

Sincerely
Mestika

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2024-09-05 16:20:04

如果您想创建一个BluetoothServerSocket,请执行此操作

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket tmp_ss = null;
try {
    tmp_ss = adapter.listenUsingRfcommWithServiceRecord(BluetoothTweetName , BlueTweetUuid);
} catch (IOException e) {
    e.printStackTrace();
}

当您创建这样的套接字时,它将等待传入连接。您无法使用它来发送或接收消息。

If you want to create a BluetoothServerSocket do this

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket tmp_ss = null;
try {
    tmp_ss = adapter.listenUsingRfcommWithServiceRecord(BluetoothTweetName , BlueTweetUuid);
} catch (IOException e) {
    e.printStackTrace();
}

When you create such a socket it will wait for incoming connection. You could not use it for send or receive messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文