我正在尝试在两个设备之间建立蓝牙连接,但到目前为止我还没能使它工作 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
答案 0 :(得分: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) { }
}
}
什么todo曾经连接?
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 ###
好的,有几件事,
首先,我认为你不需要像我一样使用碎片。我需要多个彼此沟通的活动,另一方面你的项目可能只需要一个。 (您仍然可以使用片段的概念,但要确保您的调用活动正确地调用片段)。有关片段的更多信息,请查看我的答案here。
现在, 您是否完全了解蓝牙通信的工作原理?要理解这篇关于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();
}
我制作的所选设备方法的结构是从ListView中获取设备地址。我专门创建了这个方法,所以我可以看到我试图连接的设备。您无法复制粘贴代码并期望它能够正常工作。确认您的pairedDevice集已经填充也是一个好主意。我会让你弄清楚如何确认。