gpt4 book ai didi

android - 两个设备之间的蓝牙数据传输不起作用

转载 作者:行者123 更新时间:2023-11-30 02:32:44 27 4
gpt4 key购买 nike

我正在尝试在两个设备之间建立蓝牙连接,但到目前为止我还没有成功。
ATM 我在两部手机上都有一个应用程序,根据蓝牙地址,一部应该发送信息,另一部应该接收信息。

我希望它像这样工作:在接收器手机上打开应用程序,它将等待并收听直到发生某些事情,然后在发射器手机上打开应用程序,它将连接到接收器并传输消息。

注意:该应用目前在一部手机上运行 Android 5,在另一部手机上运行 4.3。

更新:我在 SeahawksRdaBest 的帮助下尝试了一些新的东西,现在我遇到了新的错误,所以这里是新的代码和日志:

package com.example.xxxxx;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
public class BluetoothClass extends Fragment{
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice selectedDevice;

protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;

final int STATE_CONNECTED = 2;
final int STATE_CONNECTING = 1;
final int STATE_DISCONNECTED = 0;

private final UUID MY_UUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
public byte[] completeData;
double totalDistance = 0;
int wheelRotations=0;

private static final String address = "00:EE:BD:D1:66:45"; // Phone 1
private static final String address2 = "18:E2:C2:31:08:AC"; // Phone 2
private static final String TAG = "BTLOG";

Handler mHandler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
Log.i(TAG, "CONNECTED");
break;

case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
Log.v(TAG, "READING: "+readBuf.toString());
}
}
};

public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootview = inflater.inflate(R.layout.bluetooth, container,false);

rootview.findViewById(R.id.connectDevice).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
BluetoothDevice selectedDevice = null;

// If hardcoded device 1, then connect to device 2, or other way around
if (mBluetoothAdapter.getAddress().equals(address)) {
selectedDevice = selectedDevice(address2);
}
else if (mBluetoothAdapter.getAddress().equals(address2)) {
selectedDevice = selectedDevice(address);
}
mBluetoothAdapter.cancelDiscovery();
ConnectThread ct = new ConnectThread(selectedDevice);
ct.start();
}
});
return rootview;
}

public void onActivityCreared(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}

public void onStart(){
super.onStart();
}

public void onResume(){
super.onStart();
}

public void onStop(){
super.onStart();
}

// Get bluetooth device from known address
public BluetoothDevice selectedDevice(String deviceAddress){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device;
device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
return device;
}

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

public ConnectThread(BluetoothDevice device) {
mmDevice = device;

try {
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "Failed to create temporary socket");
e.printStackTrace();
}

mmSocket = tmp;
Log.i(TAG, "Finished creating socket");
}

public void run() {
mBluetoothAdapter.cancelDiscovery();

Log.i(TAG, "Connecting through socket");
try {
mmSocket.connect();
} catch (IOException connectException) {
Log.e(TAG, "Connection through socket failed: "+connectException);
Log.i(TAG, "Trying fallback method");

try {
// fallback method for android >= 4.2
tmp = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,1);
} catch (IllegalAccessException e) {
Log.e(TAG, "Failed to create fallback Illegal Access: "+e);
return;
} catch (IllegalArgumentException e) {
Log.e(TAG, "Failed to create fallback Illegal Argument: "+e);
return;
} catch (InvocationTargetException e) {
Log.e(TAG, "Failed to create fallback Invocation Target"+e);
return;
} catch (NoSuchMethodException e) {
Log.e(TAG, "Failed to create fallback No Such Method"+e);
return;
}

try {
// linked to tmp, so basicly a new socket
mmSocket.connect();
} catch (IOException e) {
Log.e(TAG, "Failed to connect with fallback socket: "+e);
return;
}

Log.i(TAG, "Succesfully connected with fallback socket");
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
return;
}
Log.i(TAG, "Succesfully connected with original socket");
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();

}
}

}

LogCat 日志:

11-23 14:03:56.281: I/BTLOG(12107): Finished creating socket
11-23 14:03:56.281: I/BTLOG(12107): Connecting through socket
11-23 14:03:56.286: D/BluetoothUtils(12107): isSocketAllowedBySecurityPolicy start : device null
11-23 14:03:56.286: W/BluetoothAdapter(12107): getBluetoothService() called with no BluetoothManagerCallback
11-23 14:03:59.696: E/BTLOG(12107): Connection through socket failed: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
11-23 14:03:59.696: I/BTLOG(12107): Trying fallback method
11-23 14:03:59.701: D/BluetoothUtils(12107): isSocketAllowedBySecurityPolicy start : device null
11-23 14:03:59.701: W/BluetoothAdapter(12107): getBluetoothService() called with no BluetoothManagerCallback
11-23 14:03:59.761: E/BTLOG(12107): Failed to connect with fallback socket: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

最佳答案

嗯,一个潜在的问题是您没有对重复消息使用任何类型的“处理程序”。你需要一个 handler反对始终处理传入流,否则您会遇到套接字关闭问题。

我的意思是:

/*
* Bluetooth Connection Threads
*/

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() {
// 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
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) {
Toast.makeText(getActivity(), "Connecting to device failed!", Toast.LENGTH_LONG).show();
}
return;
}

// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}

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

}

连接后要做什么??

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }

mmInStream = tmpIn;
mmOutStream = tmpOut;
}


public void run() {
byte[] buffer; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
// This is app specific your logic here
// Pass Handler Looper here
mHandler.obtainMessage(MESSAGE_READ, buffer).sendToTarget();
}
catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
}

处理程序:

/*
* Bluetooth Handler Method
*/
ConnectedThread connectedThread;
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// Do Something;
Toast.makeText(getActivity(),"CONNECTED",Toast.LENGTH_SHORT).show();
/*
* For loop for test values
*/
connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
listView.setVisibility(View.GONE);
connectedThread.start();
break;

case MESSAGE_READ:
// What to do with app specific data handling??
}
}
};

查看我的 github一个工作蓝牙的例子。###Update###

更新

好吧,有几件事,

首先,我认为您不需要像我一样使用 fragment 。我需要多个 Activity 相互通信,而另一方面,您的项目可能只需要一个。 (您仍然可以使用 fragment 的概念,但要确保您的调用 Activity 在调用 fragment 时正常工作)。看我的回答here有关 fragment 的更多信息。

现在, 您完全了解蓝牙通信的工作原理吗?要理解这一点,请阅读有关 java threads 的内容.因为这就是上面代码的基础。

因此,如果您仔细阅读它告诉您的日志,您正在尝试连接到一个 NULL 设备。显然你不能那样做。请记住,您必须将蓝牙设备对象传递给 ConnectThread。我相信这就是你的问题所在。您没有正确解析地址。传递正确地址的最佳方法是将两部手机相互配对。配对后,您可以从手机内存中提取正确格式的地址并将其传递给连接管理器。

试试这个:

//Define a Set, which is a unique list (don't forget to initialize it!)
Set<BluetoothDevice> pairedDevices;

在onCreateView()中,

public void onCreateView(..){
pairedDevices = mBluetoothAdapter.getBondedDevices();
// Your additional code //
/*Now you have a set containing the correct address of the paired devices to your phone,
I would recommend unpair all devices except the one you are concered with this makes life easier
as then you know the first (& only) device in the pairedDevice array is the one you want to
connect with.
*/
// Now simply pass the Set item to your ConnectThread
ConnectThread ct = new ConnectThread(pairedDevices[0])
ct.start();
}

注意事项

我制作的 Selected device 方法被构造为从 ListView 中获取设备地址。我专门创建了那个方法,这样我就可以看到我试图连接的设备。您只是不能复制粘贴代码并期望它能正常工作。确认您的 pairedDevice 集已填充也是一个好主意。我会让你弄清楚如何确认这一点。

关于android - 两个设备之间的蓝牙数据传输不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27081820/

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