Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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
Java 如何在另一个方法中调用onReceive方法?_Java_Android_Bluetooth - Fatal编程技术网

Java 如何在另一个方法中调用onReceive方法?

Java 如何在另一个方法中调用onReceive方法?,java,android,bluetooth,Java,Android,Bluetooth,目前,我开发了一种新方法来发现bluetooh就绪连接。这是我的密码: private void DiscoverOBDConnection() { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter.startDiscovery(); BroadcastReceiver mReceiver; // Create a Broadc

目前,我开发了一种新方法来发现bluetooh就绪连接。这是我的密码:

private void DiscoverOBDConnection() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mBluetoothAdapter.startDiscovery();
    BroadcastReceiver mReceiver;

    // Create a BroadcastReceiver for ACTION_FOUND
    final List<String> discoverableDevicesList = new ArrayList<String>();

    mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            // 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);
                short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
                discoverableDevicesList.add(device.getName() + "\n" + device.getAddress() + "\n" + rssi);

                String discoveredDeviceName = device.getName();
                discoverableDevicesList.add(discoveredDeviceName);
            }
        }
    };

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    context.registerReceiver(mReceiver,filter); // Don't forget to unregister during onDestroy
}
private void DiscoverOBDConnection(){
BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
广播接收机;
//为找到的操作创建广播接收器
最终列表DiscoverableDeviceList=new ArrayList();
mReceiver=新广播接收器(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
//当发现发现发现设备时
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
//从Intent获取BluetoothDevice对象
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
short-rssi=intent.getShortExtra(BluetoothDevice.EXTRA\rssi,short.MIN\u值);
DiscoverableDeviceList.add(device.getName()+“\n”+device.getAddress()+“\n”+rssi);
字符串discoveredDeviceName=device.getName();
DiscoverableDeviceList.add(discoveredDeviceName);
}
}
};
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
registerReceiver(mReceiver,filter);//不要忘记在onDestroy期间取消注册
}

我只是不知道如何从另一个方法调用onreceive方法。您能帮助我了解如何实施和发现准备好连接的蓝牙设备吗?

您只需发送广播即可

Intent i = new Intent(BluetoothDevice.ACTION_FOUND);
sendBroadcast(i);
或者直接打电话

mReceiver.onReceive(this, new Intent(BluetoothDevice.ACTION_FOUND));

你可以发送广播

Intent i = new Intent(BluetoothDevice.ACTION_FOUND);
sendBroadcast(i);
或者直接打电话

mReceiver.onReceive(this, new Intent(BluetoothDevice.ACTION_FOUND));

秀焕,谢谢你的回答。你能编辑我的密码来找到你的答案吗?我应该在哪里直接呼叫BluetoothReceiver?谢谢,苏焕公园,很好。我想出了如何实施你的建议。谢谢,太好了。但是为什么你需要自己来称呼它呢?广播由系统@The_ternalsoohwan发送,谢谢您的回答。你能编辑我的密码来找到你的答案吗?我应该在哪里直接呼叫BluetoothReceiver?谢谢,苏焕公园,很好。我想出了如何实施你的建议。谢谢,太好了。但是为什么你需要自己来称呼它呢?广播由系统@The_发送