Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android上发现蓝牙设备_Android_Bluetooth - Fatal编程技术网

在Android上发现蓝牙设备

在Android上发现蓝牙设备,android,bluetooth,Android,Bluetooth,我已经尽我所能地搜索和尝试了,但我就是不能让它工作。从我所看到的所有例子来看,我似乎在做正确的事情,但也许我遗漏了什么 我正在尝试在Android上发现蓝牙设备,并一直遵循Android开发者页面上的指南: 我可以在应用程序启动时启用BT,并获取设备的名称和地址。但是,我看不到附近其他设备的名称(应该作为祝酒词出现)。有趣的是,当我在启动应用程序之前启用BT时,它实际上实现了我想要的功能,并显示了附近设备的名称 代码如下: public class MainActivity extends Ap

我已经尽我所能地搜索和尝试了,但我就是不能让它工作。从我所看到的所有例子来看,我似乎在做正确的事情,但也许我遗漏了什么

我正在尝试在Android上发现蓝牙设备,并一直遵循Android开发者页面上的指南: 我可以在应用程序启动时启用BT,并获取设备的名称和地址。但是,我看不到附近其他设备的名称(应该作为祝酒词出现)。有趣的是,当我在启动应用程序之前启用BT时,它实际上实现了我想要的功能,并显示了附近设备的名称

代码如下:

public class MainActivity extends AppCompatActivity {

TextView mTextView;
BluetoothAdapter mBluetoothAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    final String status;

    // Check BT support
    if (mBluetoothAdapter == null){
        // Device does not support BT
        status = "Your device does not support Bluetooth";
    }
    else {
        String myAddress = mBluetoothAdapter.getAddress();
        String myName = mBluetoothAdapter.getName();
        status = myName + ": " + myAddress;
    }

    mTextView = (TextView)findViewById(R.id.textView);

    // If BT is enabled before app start, discover devices
    // This is where things work, for some reason
    if (mBluetoothAdapter.isEnabled()){
        mTextView.setText(status);
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mBroadcastReceiver, filter);
        getBTdevices();
    }

    // If BT is not enabled, enable it and look for devices
    else{
        mTextView.setText(status);
        mBluetoothAdapter.enable();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mBroadcastReceiver, filter);
        getBTdevices();
    }
}

protected void getBTdevices(){

    // Stop discovery and start
    if (mBluetoothAdapter.isDiscovering()){
        mBluetoothAdapter.cancelDiscovery();
    }
    mBluetoothAdapter.startDiscovery();

}

private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        TextView textView = (TextView)findViewById(R.id.textView2);
        textView.setText("Start");
        String action = intent.getAction();
        // If device is found
        if (BluetoothDevice.ACTION_FOUND.equals(action)){
            // Get BT device from intent
            textView.setText("Device found");
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(context, "Device found: "+ device.getName(), Toast.LENGTH_LONG).show();
            System.out.println("Discovered!");
        }
    }
};

protected void onDestroy(){
    super.onDestroy();
    if (mBluetoothAdapter.isDiscovering()){
        mBluetoothAdapter.cancelDiscovery();
    }
    unregisterReceiver(mBroadcastReceiver);

}
}
如果能帮上点忙,我真的很感激,我不知道还能做些什么


编辑:忘了提一下,我在清单中设置了蓝牙和蓝牙管理权限。

我不清楚什么工作,什么不工作……如果启动应用程序时蓝牙关闭,发现模式不会打开蓝牙适配器。要做到这一点,请参见“设置蓝牙”下的“2.启用蓝牙”。如果启动应用程序时蓝牙处于关闭状态,则我使用mBluetoothAdapter.Enable();。我用第二节的方法试过了,结果是一样的。。。工作原理:打开蓝牙启动应用程序-显示带有查找到的设备名称的toast。什么不起作用:在蓝牙关闭的情况下启动应用程序-BT被打开,但没有吐司。很抱歉,如果我一开始没有说清楚。您启用的if-else块基本上是重复的代码。。。如果在现有“if enabled”代码之前执行一个简单的“if not enabled,then enable”,该怎么办。这样,您可以保证输入第一个if语句,这是有效的。另外,我认为使用教程中提到的意图来启用蓝牙是正确的方法。我尝试了你的建议,结果也是一样的。我还将启用过程更改为指南中描述的过程,没有区别。看起来好像我的代码没有做Android系统在蓝牙开启时所做的事情。。。奇怪的