gpt4 book ai didi

java - 蓝牙启动发现未给出结果

转载 作者:行者123 更新时间:2023-12-02 01:35:55 25 4
gpt4 key购买 nike

这是我在 Android 中的第一个应用程序。我对 Android 和 Java 非常陌生。不过我是 iOS/ObjC 方面的专家。我边做边学。所以我直接开始制作一个连接蓝牙设备的应用程序。第一步当然是获取范围内可用的蓝牙设备的列表。

这是我的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest...>

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application ...

这是我的 Activity 代码:

private BluetoothAdapter btAdapter;

@Override
public void onDestroy() {
super.onDestroy();
// Unregister broadcast listeners
unregisterReceiver(mReceiver);
}


/*------------- ON CREATE ------------------------------*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
System.out.println ("Bluetooth non support");
} else {
System.out.println ("Bluetooth initialized");
}

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);

IntentFilter filterDevice = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filterDevice);

if (btAdapter.isEnabled()) {
String mydeviceaddress = btAdapter.getAddress();
String mydevicename = btAdapter.getName();

String status = mydevicename + " : " + mydeviceaddress;
System.out.println(status);
System.out.println ("Start discover");
btAdapter.startDiscovery();
} else {
System.out.println ("Not enabled");
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT, 1);
}
}


private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();

if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
System.out.println("1");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
System.out.println("2");
break;
case BluetoothAdapter.STATE_ON:
System.out.println("3");
// SCAN HERE
btAdapter.startDiscovery();
break;
case BluetoothAdapter.STATE_TURNING_ON:
System.out.println("4");
break;
}
}

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
System.out.println(device.getName() + "\n" + device.getAddress());
} else {
System.out.println("What de fuq");
}

}
};

我在 Android 手机上打开蓝牙,然后运行应用程序显示该日志:

Bluetooth initialized Start discover

仅此而已。其他日志不打印出来。知道为什么吗?我的代码看起来很完美,我不知道。

编辑:Android 检测到的蓝牙模块 HC-05 的屏幕截图。 enter image description here

最佳答案

其他设备可能未处于可发现模式。确保它们是可发现的。

关于java - 蓝牙启动发现未给出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55279965/

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