gpt4 book ai didi

安卓蓝牙无法连接

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:40:36 25 4
gpt4 key购买 nike

这个问题困扰我很久了,一直没解决。

我有一个 android 应用程序,它将所有配对的设备放在一个 ListView 中。当您单击其中一个列表项时,它将发起连接到该蓝牙设备的请求。

我可以毫无问题地获取设备列表及其地址。问题是,一旦我尝试连接,我就会在 socket.connect(); 上收到 IOException;

错误信息如下:“连接读取失败,套接字可能关闭或超时,读取 ret:-1”

这是我的代码。 任何建议将不胜感激。我非常坚持这一点。

仅供引用:“onEvent”方法是一个简化回调的库……这部分有效。当用户点击列表项时,此方法称为“public void onEvent(EventMessage.DeviceSelected event)

public class EcoDashActivity extends BaseActivity {

public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


private BluetoothAdapter mBluetoothAdapter;
private int REQUEST_ENABLE_BT = 100;
private ArrayList<BluetoothDevice> mDevicesList;
private BluetoothDeviceDialog mDialog;
private ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();


@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

mDevicesList = new ArrayList<BluetoothDevice>();

// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);

setupBluetooth();
}

private void setupBluetooth() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(this, "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
}

if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
searchForPairedDevices();
mDialog = new BluetoothDeviceDialog(this, mDevicesList);
mDialog.show(getFragmentManager(), "");
}

}

private void searchForPairedDevices() {

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mDevices.add(device.getName() + "\n" + device.getAddress());
mDevicesList.add(device);
}
}
}


private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mDevicesList.add(device);
}
}
};


@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "BT turned on!", Toast.LENGTH_SHORT).show();
searchForPairedDevices();

mDialog = new BluetoothDeviceDialog(this, mDevicesList);
mDialog.show(getFragmentManager(), "");
}
}

super.onActivityResult(requestCode, resultCode, data);
}


public void onEvent(EventMessage.DeviceSelected event) {

mDialog.dismiss();

BluetoothDevice device = event.getDevice();

ConnectThread connectThread = new ConnectThread(device);
connectThread.start();
}


public class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;

// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}

public void run() {
setName("ConnectThread");
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();

try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
Log.d("kent", "trying to connect to device");
mmSocket.connect();
Log.d("kent", "Connected!");
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
Log.d("kent", "failed to connect");

mmSocket.close();
} catch (IOException closeException) { }
return;
}

Log.d("kent", "Connected!");
}

/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}

这是我的日志。很短。

07-22 10:37:05.129: DEBUG/kent(17512): trying to connect to device
07-22 10:37:05.129: WARN/BluetoothAdapter(17512): getBluetoothService() called with no BluetoothManagerCallback
07-22 10:37:05.129: DEBUG/BluetoothSocket(17512): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[98]}
07-22 10:37:40.757: DEBUG/dalvikvm(17512): GC_CONCURRENT freed 6157K, 9% free 62793K/68972K, paused 7ms+7ms, total 72ms
07-22 10:38:06.975: DEBUG/kent(17512): failed to connect
07-22 10:38:06.975: DEBUG/kent(17512): read failed, socket might closed or timeout, read ret: -1

最后一行在 try/catch 的“Catch”部分...我只是记录错误消息。

请注意,“尝试连接设备”和“连接失败”之间大约有 20 秒的间隔

最佳答案

果冻 bean 蓝牙堆栈与其他版本明显不同。

这可能有帮助:http://wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm/

要点:UUID 是一个必须指向嵌入式设备上已发布服务的值,它不是随机生成的。您要访问的 RFCOMM SPP 连接有一个特定的 UUID,它发布该 UUID 以标识该服务,并且当您创建套接字时,它必须匹配相同的 UUID。

如果您的目标是 4.0.3 及更高版本的设备,请使用 fetchUuidsWithSdp()getUuids() 查找所有已发布的服务及其关联的 UUID 值。为了向后兼容,请阅读文章

关于安卓蓝牙无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17763779/

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