自动将蓝牙连接到最后连接的设备
Posted
技术标签:
【中文标题】自动将蓝牙连接到最后连接的设备【英文标题】:auto connect bluetooth to last connected device 【发布时间】:2013-06-14 23:48:33 【问题描述】:我正在使用蓝牙聊天示例,它非常适合配对设备并将文本从一个设备发送到另一个设备,但有时它会断开连接,我想再次与最后连接的设备重新连接。我该如何实现这一点。我有尝试从 Play 商店自动连接蓝牙,但它连接耳机和从应用程序外部而不是从内部。
如何在应用程序中实现这一点?
提前致谢。
E/BluetoothChatService(10175): accept() failed
E/BluetoothChatService(10175): java.io.IOException: Operation Canceled
E/BluetoothChatService(10175): at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:311)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:107)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:93)
E/BluetoothChatService(10175): at com.example.android.BluetoothChat.BluetoothChatService$AcceptThread.run(BluetoothChatService.java:276)
E/BluetoothChatService(10175): disconnected
E/BluetoothChatService(10175): java.io.IOException: Software caused connection abort
E/BluetoothChatService(10175): at android.bluetooth.BluetoothSocket.readNative(Native Method)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
E/BluetoothChatService(10175): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
E/BluetoothChatService(10175): at java.io.InputStream.read(InputStream.java:163)
E/BluetoothChatService(10175): at com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:436)
E/AndroidRuntime(10175): FATAL EXCEPTION: Thread-1274
E/AndroidRuntime(10175): java.lang.NullPointerException
E/AndroidRuntime(10175): at com.example.android.BluetoothChat.BluetoothChatService.connectionLost(BluetoothChatService.java:242)
E/AndroidRuntime(10175): at com.example.android.BluetoothChat.BluetoothChatService.access$6(BluetoothChatService.java:221)
E/AndroidRuntime(10175): at com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:443)E/BluetoothChat(10175): - ON PAUSE -
【问题讨论】:
我没有完全按照您的要求完成,但是您可以通过将连接的设备地址存储到共享首选项中来实现这一点,然后使用广播接收器来检测该地址是否在发现列表中。如果有,那么您可以连接它。按照此链接获取广播接收器developer.android.com/guide/topics/connectivity/bluetooth.html 感谢 Milanix 调查我的查询。我希望只发送数据,而不是在 Play 商店中的自动连接蓝牙应用程序中完成的音频/耳机。那么我该如何实现呢?您更清楚地说明了如何实现上述要求,可能是一个链接或工作示例。再次感谢您的宝贵时间。 【参考方案1】:因为我没有 eclipse atm,所以使用 gEdit 写了这个。因此,如果有一些编码错误,请不要犹豫编辑它。为了便于说明,我这里没有使用 SharedPreferences,如果你想使用请不要犹豫。
//Assuming that you have device address and is connected
private String partnerDevAdd="00:11:22:AA:BB:CC";
private boolean isConnected=true;
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(mReceiver, filter);
// Create a BroadcastReceiver for bluetooth related checks
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
//We don't want to reconnect to already connected device
if(isConnected==false)
// 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);
// Check if the found device is one we had comm with
if(device.getAddress().equals(partnerDevAdd)==true)
connectToExisting(device);
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Check if the connected device is one we had comm with
if(device.getAddress().equals(partnerDevAdd)==true)
isConnected=true;
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action))
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Check if the connected device is one we had comm with
if(device.getAddress().equals(partnerDevAdd)==true)
isConnected=false;
;
private void connectToExisting(BluetoothDevice device)
new ConnectThread(device);
ConnectThread 在此处可用http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient
【讨论】:
太棒了,这个工作!你知道如何一直连接蓝牙设备直到它连接吗??我在处理程序中使用 thread.sleep 并且它工作正常但是在它连接后循环再次运行我想结束循环我该怎么做。基本上继续运行循环直到它连接。任何想法?? new Thread(new Runnable() public void run() // TODO 自动生成的方法存根 while(true) try Thread.sleep(5000) ; catch (InterruptedException e) // TODO 自动生成的 catch 块 e.printStackTrace(); //此处的其余代码 // connect(); ).start(); 如何结束上述循环?? 如果您仔细查看文档,它说 BluetoothSocket.connect() 是一个阻塞调用,所以它要么连接要么失败。您可以修改该代码块并移动返回;如果您想一次只与一个客户端通信,请从 try/catch 块中将其移至 manageConnectedSocket(mmSocket) 下方。如果不是这种情况,则无需停止该线程。而且你不需要循环来检查。 上面的代码在没有while循环的情况下工作,但是如果我实现while循环,它会一次又一次地连接。我想在连接后结束线程,但每次我这样做时应用程序都会崩溃! !你知道我怎样才能结束循环吗?我尝试了很多方法布尔检查和东西,但它仍然崩溃..以上是关于自动将蓝牙连接到最后连接的设备的主要内容,如果未能解决你的问题,请参考以下文章