2011-05-23 34 views
2

我正在嘗試實現遊戲的藍牙多人遊戲功能。但是連接存在問題。這很混亂。我使用Android的示例代碼,因爲我從來沒有嘗試過這樣的事情。與示例代碼的藍牙連接問題

該示例是一個簡單的藍牙聊天。剛纔我嘗試再次配對這些設備。

(至少是Android 2.1)

摩托羅拉FLIPOUT 索尼愛立信X10迷你 HTC傳奇

如果FLIPOUT正在掃描的另一設備併發送配對請求一切正常工作。如果其他兩個設備嘗試連接到FlipOut,則配對請求會出現在一個設備上。點擊後沒有發生任何事情。幾秒鐘後,我收到Toast消息「無法連接到設備

我對TicTacToe使用相同的代碼但行爲發生了變化FlipOut作爲主機沒有任何問題但FlipOut不能連接到其他設備最後一天,我嘗試了很多設備,例如三星Galaxy S,索尼愛立信X8,索尼愛立信X10 ...

我找不到規律性,我讀了三星和HTC有問題方法「listenUsingRfcommWithServiceRecord」。但它應該已經修正了二月。

有人可以解釋爲什麼它不能正常工作,以及如何解決它。如果我去設置並嘗試建立連接,一切工作正常。那意味着即使示例代碼工作不正常,也必須有解決方案嗎?

我不確定它是否有助於找到解決方案。但我安裝了遊戲「Galaxir」,這是一款來自Android Market的帶藍牙多人遊戲功能的應用程序。而且它也不完美。

回答

1

嘗試使用此代碼爲Socket連接,而不是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)); 
+0

這樣,應用程序崩潰,如果它試圖連接到設備。 (在我選擇應用程序應連接的設備後) – Xazen 2011-05-23 09:43:21

1

藍牙例如我跑沒有任何工作。這是因爲他們沒有在清單中聲明服務。

用下面的代碼替換你的清單,它應該工作。

<?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>