示例代码的蓝牙连接问题

发布于 2024-11-09 17:55:17 字数 671 浏览 0 评论 0原文

我正在尝试为游戏实现蓝牙多人游戏功能。但连接存在问题。这很令人困惑。我正在使用 Android 的示例代码,因为我以前从未尝试过这样的事情。

该示例是一个简单的BluetoothChat。刚才我尝试再次配对这些设备。

(至少 Android 2.1)

摩托罗拉 FlipOut 索尼爱立信 X10 迷你 HTC Legends

如果 FlipOut 正在扫描另一台设备并发送配对请求,则一切正常。如果另外两台设备尝试连接到 FlipOut,其中一台设备上会出现配对请求。点击配对后没有任何反应。几秒钟后,我收到一条 Toast 消息“无法连接到设备。

我对 TicTacToe 使用相同的代码。但行为发生了变化。FlipOut 作为主机没有任何问题。但 FlipOut 无法连接到其他设备最近几天我尝试了很多设备,例如三星 Galaxy S、索尼爱立信 X8、索尼爱立信 X10...

我找不到规律,我读到三星和 HTC 的“listenUsingRfcommWithServiceRecord”方法有问题。已于二月修复。

有人可以解释为什么它无法正常工作以及如何修复它吗?如果我进入设置并尝试建立连接,这意味着必须有解决方案。代码不能完美运行?

我不确定它是否有助于找到解决方案,但我安装了来自 Android Market 的具有蓝牙多人游戏功能的游戏“Galaxir”,但它也不能完美运行。

I am trying to implement a bluetooth multiplayer feature for a game. But there are problems with the connection. It is confusing. I am using the sample code of Android since I never tried something like this before.

The sample is a simple BluetoothChat. Just now I tried to pair these devices one more time.

(At least Android 2.1)

Motorola FlipOut
SonyEricsson X10 mini
HTC Legends

If the FlipOut is scanning for another device and sends a pairing request everything work fine. If the other two devices try to connect to the FlipOut a pairing request appear on one device. After clicking on pairing nothing happends. After a few seconds I got a Toast-message" unable to connect to device.

I use the same code for my TicTacToe. But the behavior changes. The FlipOut works as host without any problems. But the FlipOut can't connect to other devices. The last days I tried many devices. e.g. Samsung Galaxy S, Sony Ericsson X8, Sony Ericsson X10...

I can't find a regularity. I read Samsung and HTC had a problem with the method "listenUsingRfcommWithServiceRecord". But it should have been fixed February.

Can someone explain why it won't work properly and how I can fix it. If I go to settings and try to make a connection, everything works fine. That means there have to be solution, even though the sample code isn't working perfectly?

I am not sure wether it helps to find a solution. But I install the game "Galaxir", an app from the Android Market with Bluetooth multiplayer feature. And it doesn't work flawless as well.

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

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

发布评论

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

评论(2

假情假意假温柔 2024-11-16 17:55:17

尝试使用此代码进行套接字连接,而不是 createRfcommSocketToServiceRecord()

BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
Method m;
m = hxm.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
socket = (BluetoothSocket)m.invoke(hxm, Integer.valueOf(1));

Try to use this code for Socket Connection instead of createRfcommSocketToServiceRecord()

BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
Method m;
m = hxm.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
socket = (BluetoothSocket)m.invoke(hxm, Integer.valueOf(1));
萤火眠眠 2024-11-16 17:55:17

我运行的蓝牙示例也不起作用。这是因为他们没有在清单中声明该服务。

将您的清单替换为以下代码,它应该可以工作。

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.android.BluetoothChat"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk minSdkVersion="6" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

    <application android:label="@string/app_name"
                 android:icon="@drawable/app_icon" android:debuggable="true">
        <activity android:name=".BluetoothChat"
                  android:label="@string/app_name"
                  android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="BluetoothChatService">   
        </service>
        <activity android:name=".DeviceListActivity"
                  android:label="@string/select_device"
                  android:theme="@android:style/Theme.Dialog"
                  android:configChanges="orientation|keyboardHidden" />
    </application>
</manifest>

The bluetooth example I was running wasn't working either. It was because they didn't declare the service in the manifest.

Replace your manifest with the following code and it should work.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.android.BluetoothChat"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk minSdkVersion="6" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

    <application android:label="@string/app_name"
                 android:icon="@drawable/app_icon" android:debuggable="true">
        <activity android:name=".BluetoothChat"
                  android:label="@string/app_name"
                  android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="BluetoothChatService">   
        </service>
        <activity android:name=".DeviceListActivity"
                  android:label="@string/select_device"
                  android:theme="@android:style/Theme.Dialog"
                  android:configChanges="orientation|keyboardHidden" />
    </application>
</manifest>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文