英文:
How to connect paired bluetooth device on app startup in Android Studio?
问题
以下是你要求的翻译内容:
有没有办法在应用启动时通过蓝牙LE自动连接特定设备?
我已经在过去的几个小时里在Stack Overflow上浏览了一些内容,看到了许多类似的问题,尽管大部分都相当旧,涉及到反射或其他我无法完全理解的复杂方法(我已经尝试实现这些方法,但并没有成功,因为我真的不明白其中发生了什么)。
到目前为止,我已经成功通过友好名称找到了设备,尽管我不知道在那个if语句中应该执行什么。这是在我的MainActivity中:
protected void onCreate(Bundle savedInstanceState) {
...
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"不支持蓝牙",Toast.LENGTH_SHORT).show();
} else {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if(pairedDevices.size()>0){
for(BluetoothDevice device: pairedDevices){
if (deviceName.equals(device.getName())) {
//找到设备!
//现在我该如何配对它?
break;
}
...
注意:在翻译过程中,我保留了代码中的注释和标记(如//
和/*...*/
),并进行了相应的翻译。
英文:
Is there any way to automatically connect a specific device via Bluetooth LE on app startup?
I've been scrolling through stack overflow for the past few hours and have seen a number of similar questions, although majority are quite outdated and deal with reflections or other complex methods that I can't quite comprehend (these methods I've tried to implement, but not successfully, as I didn't really understand what was going on).
So far, I've managed to find the device by its friendly name, although I have no clue what to execute in that if statement. This is within my MainActivity:
protected void onCreate(Bundle savedInstanceState) {
...
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Bluetooth not supported",Toast.LENGTH_SHORT).show();
} else {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if(pairedDevices.size()>0){
for(BluetoothDevice device: pairedDevices){
if (deviceName.equals(device.getName())) {
//Device found!
//Now how do I pair it?
break;
}
...
答案1
得分: 0
假设您已成功识别出 BlueToothDevice
,现在您需要连接到 GATT(通用属性配置文件),以便进行数据传输。
<br>
<br>
使用 BlueToothDevice.connectGatt
方法。使用第一个重载,该方法接受一个 Context
、一个布尔值(false = 直接连接,true = 在可用时连接),以及一个 BlueToothGattCallback
。回调函数从设备接收信息。
BlueToothGatt blueToothGatt = device.connectGatt(this, false, blueToothGattCallback);
以下是实现回调的示例代码:
BluetoothGattCallback blueToothGattCallback =
new BluetoothGattCallback()
{
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if(newState == BlueToothProfile.STATE_CONNECTED){
/* 做一些操作 */
}
}
}
有关回调的更多详细信息,请参阅此处。
英文:
Assuming you've successfully identified the BlueToothDevice
, you now need to connect to the GATT(Generic Attribute Profile), which allows you to transfer data.
<br>
<br>
Use the BlueToothDevice.connectGatt
method. Using the first overload, the method takes in a Context
, a boolean (false = directly connect, true = connect when available), and a BlueToothGhattCallback
. The callback receives info from the device.
BlueToothGatt blueToothGatt = device.connectGatt(this, false, blueToothGattCallback);
An example to implement the callback:
BluetoothGattCallback blueToothGattCallback =
new BluetoothGattCallback()
{
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if(newState == BlueToothProfile.STATE_CONNECTED){
/* do stuff */
}
}
}
More details on the callbacks here.
答案2
得分: 0
浏览了此应用程序的源代码,特别是SerialSocket、SerialService和SerialListener文件,完全解决了我的问题。
英文:
Ended up scrolling through the source code for this app, particularly the SerialSocket, SerialService and SerialListener files which completely solved my problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论