我正在使用Android 2.1 sdk和Motorolad Droid A855型号,当尝试连接蓝牙(Socket SPP)时,我收到以下错误,如logcat, 并在socket.conect();
中获得异常抛出 09-07 05:03:40.874: ERROR/BluetoothEventLoop.cpp(1102): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/1142/hci0/dev_00_0E_6D_BA_89_F3
09-07 05:03:41.217: DEBUG/BluetoothService(1102): updateDeviceServiceChannelCache(00:0E:6D:BA:89:F3)
09-07 05:03:41.233: DEBUG/BluetoothService(1102): Cleaning up failed UUID channel lookup: 00:0E:6D:BA:89:F3 550e8400-e29b-41d4-a716-446655440000
09-07 05:03:41.241: INFO/BTconnect(1650): Step 7
09-07 05:03:41.241: ERROR/--------Inner Exception-----(1650): Socket not conncected
09-07 05:03:41.256: VERBOSE/BluetoothEventRedirector(1660): Received android.bleutooth.device.action.UUID
09-07 05:03:41.327: INFO/ActivityManager(1102): Displayed activity com.sample.BT/.BluetoothDemo: 947 ms (total 947 ms)
09-07 05:03:45.038: ERROR/BluetoothEventLoop.cpp(1102): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/1142/hci0/dev_00_0E_6D_BA_89_F3
09-07 05:03:46.499: VERBOSE/BluetoothEventRedirector(1660): Received android.bleutooth.device.action.UUID
这是我的代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
out = (TextView) findViewById(R.id.out);
// Getting the Bluetooth adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
out.append("\nAdapter: " + mBluetoothAdapter);
UUID uuid = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");
if(mBluetoothAdapter==null) {
out.append("\nBluetooth NOT supported. Aborting.");
return;
}
Log.i("BTDemo", ": "+ mBluetoothAdapter.isEnabled());
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else
{
out.append("\nBluetooth Adapter Enabled");
}
try {
BluetoothDevice devices=mBluetoothAdapter.getRemoteDevice("00:0E:6D:BA:89:F3");
if(devices!=null)
{
socket = devices.createRfcommSocketToServiceRecord(uuid);
Log.i("In Socket","Socket"+socket);
Log.i("BTconnect", "Step 4");
}
else
{
Log.e("BTConnect","Bluetooth device is null");
return ;
}
Log.i("BTconnect", "Step 5");
// Listing paired devices
out.append("\nDevices Pared:");
//BluetoothDevice devices;
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device: pairedDevices)
{
out.append("\nFound device: " + device);
}
} catch (Exception e) {
e.printStackTrace();
}
try{
if(socket !=null) // checking for socket is created or not
{
socket.connect();
Log.i("BTconnect", "Step 6");
Log.i("BTconnect", "Socket Connected");
out.append("\n Socket Connected");
}
else
{
out.append("\n\n Socket is Null");
}
}catch(Exception e)
{
Log.i("BTconnect", "Step 7");
Log.e("--------Inner Exception-----", "Socket not conncected");
out.append("\n\n\n --------Inner Exception----- Socket not conncected");
try {
socket.close();
out.append("\n\n Socket closed");
} catch (IOException closeException)
{
Log.e("InitBluetoothDevice","while closing socket in catch block of connect()"+closeException.getMessage());
out.append("\n\n --------Exception----- while closing socket");
}
}
}
并且清单文件是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.BT"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BluetoothDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
</manifest>
我尝试使用不同的UUID连接,但没有成功
以下是我从Logcat获得的异常:
09-07 06:24:14.077: VERBOSE/BluetoothEventRedirector(1649): Received android.bleutooth.device.action.UUID
09-07 06:24:14.077: DEBUG/BluetoothService(1098): Cleaning up failed UUID channel lookup: 00:0E:6D:BA:89:F3 550e8400-e29b-41d4-a716-446655440000
09-07 06:24:14.085: ERROR/--------Inner Exception-----(1769): Socket not conncected
09-07 06:24:14.085: WARN/System.err(1769): java.io.IOException: Service discovery failed
09-07 06:24:14.101: WARN/System.err(1769): at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:377)
09-07 06:24:14.101: WARN/System.err(1769): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:201)
09-07 06:24:14.101: WARN/System.err(1769): at com.sample.BT.BluetoothDemo.onCreate(BluetoothDemo.java:89)
09-07 06:24:14.101: WARN/System.err(1769): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 06:24:14.101: WARN/System.err(1769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 06:24:14.101: WARN/System.err(1769): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 06:24:14.101: WARN/System.err(1769): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 06:24:14.101: WARN/System.err(1769): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 06:24:14.101: WARN/System.err(1769): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 06:24:14.101: WARN/System.err(1769): at android.os.Looper.loop(Looper.java:123)
09-07 06:24:14.101: WARN/System.err(1769): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 06:24:14.101: WARN/System.err(1769): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 06:24:14.101: WARN/System.err(1769): at java.lang.reflect.Method.invoke(Method.java:521)
09-07 06:24:14.101: WARN/System.err(1769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 06:24:14.109: WARN/System.err(1769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 06:24:14.109: WARN/System.err(1769): at dalvik.system.NativeStart.main(Native Method)
09-07 06:24:14.249: INFO/ActivityManager(1098): Displayed activity com.sample.BT/.BluetoothDemo: 3640 ms (total 3640 ms)
09-07 06:24:16.874: VERBOSE/BluetoothEventRedirector(1649): Received android.bleutooth.device.action.UUID
09-07 06:24:18.046: ERROR/BluetoothEventLoop.cpp(1098): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/1137/hci0/dev_00_0E_6D_BA_89_F3
一些人可以帮助连接蓝牙,