gpt4 book ai didi

android - SPP Socket createRfcommSocketToServiceRecord 将无法连接

转载 作者:行者123 更新时间:2023-11-29 00:50:55 25 4
gpt4 key购买 nike

我想使用 Android 2.1 连接到提供 SPP 端口的外部蓝牙设备大部头书。在这种情况下,它是一个外部 GPS 单元。当我尝试连接时,处于“客户端”模式时无法连接已建立的套接字。然后,如果我尝试设置一个套接字(处于服务器角色),从我的 PC 接收文本,一切正常。这计算机可以使用 SSP UUID 或一些随机 UUID 通过 SPP 作为客户端连接到手机上的套接字。所以问题不在于我使用了错误的 UUID。

但反过来(例如,在已建立的客户端套接字上调用连接)createRfcommSocketToServiceRecord(UUID uuid))就是行不通的。可悲的是我没有时间进一步检查问题。如果有人能指出我正确的方法,那就太好了。手机:三星 Galaxy Spica I-5700 Android 2.1日志文件的以下部分必须是问题所在。

问候 PhilDev

附言我会在办公时间在场。

这里是日志文件:


03-21 03:10:52.020: DEBUG/BluetoothSocket.cpp(4643): initSocketFromFdNative
03-21 03:10:52.025: DEBUG/BluetoothSocket(4643): connect
03-21 03:10:52.025: DEBUG/BluetoothSocket(4643): doSdp
03-21 03:10:52.050: DEBUG/ADAPTER(2132): create_device(01:00:00:7F:B5:B3)
03-21 03:10:52.050: DEBUG/ADAPTER(2132): adapter_create_device(01:00:00:7F:B5:B3)
03-21 03:10:52.055: DEBUG/DEVICE(2132): Creating device [address = 01:00:00:7F:B5:B3] /org/bluez/2132/hci0/dev_01_00_00_7F_B5_B3 [name = ]
03-21 03:10:52.055: DEBUG/DEVICE(2132): btd_device_ref(0x10c18): ref=1
03-21 03:10:52.065: INFO/BluetoothEventLoop.cpp(1914): event_filter: Received signal org.bluez.Adapter:DeviceCreated from /org/bluez/2132/hci0
03-21 03:10:52.065: INFO/BluetoothService.cpp(1914): ... Object Path = /org/bluez/2132/hci0/dev_01_00_00_7F_B5_B3
03-21 03:10:52.065: INFO/BluetoothService.cpp(1914): ... Pattern = 00001101-0000-1000-8000-00805f9b34fb, strlen = 36
03-21 03:10:52.070: DEBUG/DEVICE(2132): *************DiscoverServices********
03-21 03:10:52.070: INFO/DTUN_HCID(2132): dtun_client_get_remote_svc_channel: starting discovery on (uuid16=0x0011)
03-21 03:10:52.070: INFO/DTUN_HCID(2132): bdaddr=01:00:00:7F:B5:B3
03-21 03:10:52.070: INFO/DTUN_CLNT(2132): Client calling DTUN_METHOD_DM_GET_REMOTE_SERVICE_CHANNEL (id 4)
03-21 03:10:52.070: INFO/(2106): DTUN_ReceiveCtrlMsg: [DTUN] Received message [BTLIF_DTUN_METHOD_CALL] 4354
03-21 03:10:52.070: INFO/(2106): handle_method_call: handle_method_call :: received DTUN_METHOD_DM_GET_REMOTE_SERVICE_CHANNEL (id 4), len 134
03-21 03:10:52.075: ERROR/BTLD(2106): ****************search UUID = 1101***********
03-21 03:10:52.075: INFO//system/bin/btld(2103): btapp_dm_GetRemoteServiceChannel()
03-21 03:10:52.120: DEBUG/BluetoothService(1914): updateDeviceServiceChannelCache(01:00:00:7F:B5:B3)
03-21 03:10:52.120: DEBUG/BluetoothEventLoop(1914): ClassValue: null for remote device: 01:00:00:7F:B5:B3 is null
03-21 03:10:52.120: INFO/BluetoothEventLoop.cpp(1914): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/2132/hci0
03-21 03:10:52.305: WARN/BTLD(2106): bta_dm_check_av:0
03-21 03:10:56.395: DEBUG/WifiService(1914): ACTION_BATTERY_CHANGED pluggedType: 2
03-21 03:10:57.440: WARN/BTLD(2106): SDP - Rcvd conn cnf with error: 0x4 CID 0x43
03-21 03:10:57.440: INFO/BTL-IFS(2106): send_ctrl_msg: [BTL_IFS CTRL] send BTLIF_DTUN_SIGNAL_EVT (CTRL) 13 pbytes (hdl 10)
03-21 03:10:57.445: INFO/DTUN_CLNT(2132): dtun-rx signal [DTUN_SIG_DM_RMT_SERVICE_CHANNEL] (id 42) len 15
03-21 03:10:57.445: INFO/DTUN_HCID(2132): dtun_dm_sig_rmt_service_channel: success=1, service=00000000
03-21 03:10:57.445: ERROR/DTUN_HCID(2132): discovery unsuccessful!

package de.phil_dev.android.BT;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class ThinBTClient extends Activity {

private static final String TAG = "THINBTCLIENT";
private static final boolean D = true;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private BufferedInputStream inStream = null;
private BluetoothServerSocket myServerSocket;
private ConnectThread myConnection;
private ServerThread myServer;
// Well known SPP UUID (will *probably* map to
// RFCOMM channel 1 (default) if not in use);
// see comments in onResume().
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");


// .fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee");

// ==> hardcode your slaves MAC address here <==
// PC
// private static String address = "00:09:DD:50:86:A0";

// GPS
private static String address = "00:0B:0D:8E:D4:33";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

if (D)
Log.e(TAG, "+++ ON CREATE +++");

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available.",
Toast.LENGTH_LONG).show();
finish();
return;
}

if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this,
"Please enable your BT and re-run this program.",
Toast.LENGTH_LONG).show();
finish();
return;
}

if (D)
Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");
}

@Override
public void onStart() {
super.onStart();
if (D)
Log.e(TAG, "++ ON START ++");
}

@Override
public void onResume() {
super.onResume();

if (D) {
Log.e(TAG, "+ ON RESUME +");
Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");
}

// Make the phone discoverable

// When this returns, it will 'know' about the server,
// via it's MAC address.
// mBluetoothAdapter.startDiscovery();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
Log.e(TAG, device.getName() + " connected");


// myServer = new ServerThread();
// myServer.start();

myConnection = new ConnectThread(device);
myConnection.start();

}

@Override
public void onPause() {
super.onPause();

if (D)
Log.e(TAG, "- ON PAUSE -");

try {
btSocket.close();
} catch (IOException e2) {
Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);
}
}

@Override
public void onStop() {
super.onStop();
if (D)
Log.e(TAG, "-- ON STOP --");
}

@Override
public void onDestroy() {
super.onDestroy();
if (D)
Log.e(TAG, "--- ON DESTROY ---");
}

private class ServerThread extends Thread {
private final BluetoothServerSocket myServSocket;

public ServerThread() {
BluetoothServerSocket tmp = null;

// create listening socket
try {
tmp = mBluetoothAdapter
.listenUsingRfcommWithServiceRecord(
"myServer", MY_UUID);
} catch (IOException e) {
Log.e(TAG, "Server establishing failed");
}

myServSocket = tmp;
}

public void run() {
Log.e(TAG, "Beginn waiting for connection");
BluetoothSocket connectSocket = null;

InputStream inStream = null;
byte[] buffer = new byte[1024];
int bytes;

while (true) {
try {
connectSocket = myServSocket.accept();
} catch (IOException e) {
Log.e(TAG, "Connection failed");
break;
}

Log.e(TAG, "ALL THE WAY AROUND");
try {
connectSocket = connectSocket.getRemoteDevice()
.createRfcommSocketToServiceRecord(MY_UUID);
connectSocket.connect();
} catch (IOException e1) {
Log.e(TAG, "DIDNT WORK");
}

// handle Connection
try {
inStream = connectSocket.getInputStream();
while (true) {
try {
bytes = inStream.read(buffer);
Log.e(TAG, "Received: " + buffer.toString());
} catch (IOException e3) {
Log.e(TAG, "disconnected");
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
}

}
}

void cancel() {

}
}

private class ConnectThread extends Thread {

private final BluetoothSocket mySocket;
private final BluetoothDevice myDevice;

public ConnectThread(BluetoothDevice device) {
myDevice = device;
BluetoothSocket tmp = null;

try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "CONNECTION IN THREAD DIDNT WORK");
}
mySocket = tmp;
}

public void run() {
Log.e(TAG, "STARTING TO CONNECT THE SOCKET");
setName("My Connection Thread");
InputStream inStream = null;
boolean run = false;
//mBluetoothAdapter.cancelDiscovery();

try {
mySocket.connect();
run = true;
} catch (IOException e) {
run = false;
Log.e(TAG, this.getName()
+ ": CONN DIDNT WORK, Try closing socket");
try {
mySocket.close();
} catch (IOException e1) {
Log.e(TAG, this.getName() + ": COULD CLOSE SOCKET", e1);

this.destroy();
}
}

synchronized (ThinBTClient.this) {
myConnection = null;
}

byte[] buffer = new byte[1024];
int bytes;
// handle Connection
try {
inStream = mySocket.getInputStream();
while (run) {

try {
bytes = inStream.read(buffer);
Log.e(TAG, "Received: " + buffer.toString());
} catch (IOException e3) {
Log.e(TAG, "disconnected");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// starting connected thread (handling there in and output
}

public void cancel() {
try {
mySocket.close();
} catch (IOException e) {
Log.e(TAG, this.getName() + " SOCKET NOT CLOSED");
}
}

}

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.phil_dev.android.BT" android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET"/>

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".ThinBTClient" 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" />

</manifest>

最佳答案

我发现当其中一台设备的蓝牙关闭时会发生这个问题。

关于android - SPP Socket createRfcommSocketToServiceRecord 将无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2519449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com