Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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 如何从RedBearLab';s聊天的例子?_Java_Android_Arduino_Bluetooth Lowenergy - Fatal编程技术网

Java 如何从RedBearLab';s聊天的例子?

Java 如何从RedBearLab';s聊天的例子?,java,android,arduino,bluetooth-lowenergy,Java,Android,Arduino,Bluetooth Lowenergy,我一直在尝试修改Android Studio中的RedBearLab聊天程序,将我自己的数据发送到我的Arduino Uno上的BLE盾牌 appstore中的RedBearLab应用程序显示Bluetooth low Engery找到的设备的Rssi值,而聊天示例(它只是你可以在appstore上获得的整个应用程序的一部分)仅显示设备名称和地址 在研究了几种不同的解决方案以获得rssi值后,我发现有两种方法可以获得这些值: -扫描带有lescan回调函数的设备时 -在手机和设备之间建立连接后

我一直在尝试修改Android Studio中的RedBearLab聊天程序,将我自己的数据发送到我的Arduino Uno上的BLE盾牌

appstore中的RedBearLab应用程序显示Bluetooth low Engery找到的设备的Rssi值,而聊天示例(它只是你可以在appstore上获得的整个应用程序的一部分)仅显示设备名称和地址

在研究了几种不同的解决方案以获得rssi值后,我发现有两种方法可以获得这些值:
-扫描带有lescan回调函数的设备时
-在手机和设备之间建立连接后

现在我想知道的是如何从lescan回调函数中获取rssi值。我发现了不同的解决方案,将值与设备名称一起发送到另一个函数(通常)到另一个类,在该类中,该值附加到一个文本字段

当我知道这个值时,我想用它来缩小应用程序发现的设备范围,只添加高于某个rssi值的设备(比如说低于-70太远了)

private BluetoothAdapter.LeScanCallback mLeScanCallback=新建BluetoothAdapter.LeScanCallback(){

上面的代码在Main.java中

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

    setTitle("Device");

    listView = (ListView) findViewById(R.id.listView);

    devices = (ArrayList<BluetoothDevice>) Main.mDevices;
    for (BluetoothDevice device : devices) {
        map = new HashMap<String, String>();
        map.put(DEVICE_NAME, device.getName());
        map.put(DEVICE_ADDRESS, device.getAddress());
        listItems.add(map);
    }

    adapter = new SimpleAdapter(getApplicationContext(), listItems,
            R.layout.list_item, new String[] { "name", "address" },
            new int[] { R.id.deviceName, R.id.deviceAddr });
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

所以我尝试在RedBearLab的聊天代码中实现它,但它不起作用,我也不知道它在youtube教程中是如何工作的。

看看stackoverflow,很可能这个问题已经得到了回答。

看看stackoverflow,很可能这个问题已经得到了回答。

是的,所以这段代码使用了第一种解决方案,但只显示了设备名称(还有地址,不知何故我不完全理解).现在我得到了第二个值是我想要的rssi值,但我没有得到的是如何将其取出,并将其与设备名一起发送到设备,java类,将其放在arraylist中。如何获取rssi值?使用
getRssi()
ScanResult
对象中的函数。如何将其发送回?定义一个接口,当找到结果时将调用其函数,并将其发送到
Device.java
类。getRssi()不是吗从第二个解决方案来看,如果已经使用第一个解决方案来获取设备名称,这可能不起作用?我尝试输入onScanResults,但我的代码不知道getRssi()方法在深入研究Android BluetoothLE API之后,我发现有很多方法。在API>=17中,当找到设备时,回调是
public void onLeScan(BluetoothDevice设备,int rssi,byte[]scanRecord)
。您可以通过调用
device.getName()来获取设备名称
和RSSI值作为函数的参数提供。在API>=21上,回调与ScanResult(int callbackType,ScanResult)上的public void不同。
。要获取名称调用,请调用
result.getScanRecord().getDeviceName()
和RSSI值
result.getRssi()
。聊天代码似乎使用的是API>=17回调。我发现的是,您可以在哪里找到对回调的解释。当您搜索rssi时(ctrl+f)提到了额外的RSSI,并找到了行动。我不确定这是否是解决问题的线索,因为我不知道如何实现它。是的,所以这段代码使用了第一个解决方案,但只给出了设备名称(还有地址,不知怎的,我不完全理解).现在我得到了第二个值是我想要的rssi值,但我没有得到的是如何将其取出,并将其与设备名一起发送到设备,java类,将其放在arraylist中。如何获取rssi值?使用
getRssi()
ScanResult
对象中的函数。如何将其发送回?定义一个接口,当找到结果时将调用其函数,并将其发送到
Device.java
类。getRssi()不是吗从第二个解决方案来看,如果已经使用第一个解决方案来获取设备名称,这可能不起作用?我尝试输入onScanResults,但我的代码不知道getRssi()方法在深入研究Android BluetoothLE API之后,我发现有很多方法。在API>=17中,当找到设备时,回调是
public void onLeScan(BluetoothDevice设备,int rssi,byte[]scanRecord)
。您可以通过调用
device.getName()来获取设备名称
和RSSI值作为函数的参数提供。在API>=21上,回调与ScanResult(int callbackType,ScanResult)上的public void不同。
。要获取名称调用,请调用
result.getScanRecord().getDeviceName()
和RSSI值
result.getRssi()
。聊天代码似乎在使用API>=17回调。我发现的是回调的解释。当你搜索rssi(ctrl+f)时,会提到额外的rssi并找到操作。我不确定这是否是解决问题的线索,因为我不知道如何实现它。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.device_list);

    setTitle("Device");

    listView = (ListView) findViewById(R.id.listView);

    devices = (ArrayList<BluetoothDevice>) Main.mDevices;
    for (BluetoothDevice device : devices) {
        map = new HashMap<String, String>();
        map.put(DEVICE_NAME, device.getName());
        map.put(DEVICE_ADDRESS, device.getAddress());
        listItems.add(map);
    }

    adapter = new SimpleAdapter(getApplicationContext(), listItems,
            R.layout.list_item, new String[] { "name", "address" },
            new int[] { R.id.deviceName, R.id.deviceAddr });
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

                final int new_rssi = rssi;
                if (rssi > signalStrength) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            ma.addDevice(device, new_rssi);
                        }
                    });
                }
            }
        };