English 中文(简体)
青to开始发现没有结果
原标题:Bluetooth start discovery not giving results

这是我第一次在安康。 我是安东和贾瓦的新鲜事。 不过,我是SOS/ObjC的专家。 我正在学习。 因此,我跳了直截了当,把一个蓝色装置连接起来。 第一步当然是获得一个范围可达的蓝色装置清单。

我在此表明:

<?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 ...

我的活动守则如下:

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

        }
    };

我在我的和roid的电话上转 on蓝色,然后用 app显示:

Bluetooth initialized Start discover

所有这一切。 其他标志没有打印。 任何想法为什么? 我的法典似乎完美无缺。

EDIT: Screenshot of the Bluetooth module HC-05 being detected by Android. enter image description here

问题回答

其他装置可能无法发现。 确保他们能够发现。

如果你的其他装置是蓝色的单元,那么就Arduino而言,这是正确的吗?

If so, check this tutorial describing connection between Android device and HC05 module. bthc-05 to android tutorial

此外,根据这一官方样本:google 样本——蓝色聊天

或者,你也可以采用以下方法发现你的装置。 在两部电话上安装。 然后,你可以相互在东边发现电话。

protected void makeDiscoverable(){
    // Make local device discoverable
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVERABLE_DURATION);
    startActivityForResult(discoverableIntent, DISCOVERABLE_BT_REQUEST_CODE);
}

愿这些帮助!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签