Bluetooth connection problem with sample code
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 c开发者_开发技巧onnect 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.
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));
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>
精彩评论