在Android手机上获取Android Wear的蓝牙RSSI [英] Get Bluetooth RSSI of Android Wear on an Android phone

查看:111
本文介绍了在Android手机上获取Android Wear的蓝牙RSSI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过蓝牙RSSI确定Android手机(Nexus 5X)和Android Wear设备(Samsung Gear live)之间的距离.

I would like to determine the distance between an Android phone (Nexus 5X) and an Android Wear device (Samsung Gear live) by means of the Bluetooth RSSI.

我找到了使用蓝牙功能readRemoteRssi();的解决方案,如

I found solutions to use the Bluetooth function readRemoteRssi(); as in

final Runnable runnable = new Runnable() {  
        @Override
        public void run() {
            // TODO Auto-generated method stub
            mBluetoothLeService.readRemoteRssi();
            mHandler.postDelayed(this, 1000);
        }
    };

但是,在使用Android Wear时,无需使用蓝牙管理器,适配器或GATT服务;我还没有看到它们在可穿戴示例项目中进行初始化.这是我的问题:配对后,如何在约5秒的间隔内从手机上的Android Wear获取蓝牙RSSI值.有人可以指出我的示例代码大致执行我打算做的事情吗?

However, when using Android Wear, it is not necessary to use Bluetooth managers, adapters or GATT services; I have not seen them initialised in the wearable sample projects. Here is my question: How do I get the Bluetooth RSSI value from the Android Wear on my phone after pairing, in an interval of about 5 seconds. Can someone point me to example code doing roughly what I am planning to do?

我找到了许多代码示例,并得到了以下MainActivity.我能够获得已配对设备的列表,然后开始发现并使用BroadcastReceiver尝试读取RSSI值.

I have found a number of code samples and ended up with the following MainActivity. I am able to get a list of the paired devices, then I start a discovery and using a BroadcastReceiver try to read out the RSSI value.

package com.tudelft.androidweardistance;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<String> mDeviceList = new ArrayList<String>();
    private BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        listView = (ListView) findViewById(R.id.listView);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        getPairedDevices();
        startDiscovery();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void discover(View view){
        getPairedDevices();
        startDiscovery();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void startDiscovery() {
        mBluetoothAdapter.cancelDiscovery();
        mBluetoothAdapter.startDiscovery();
    }

    private void getPairedDevices() {
        Set<BluetoothDevice> devicesArray=mBluetoothAdapter.getBondedDevices();
        if(devicesArray.size()>0){
            for(BluetoothDevice device:devicesArray){
                mDeviceList.add(device.getName());
                System.out.println(device.getName());
            }
        }
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)) {
                int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
                Toast.makeText(getApplicationContext(), "  RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show();
                System.out.println(rssi);
            }
        }
    };

}

可能是Samsung Gear Live与RSSI服务不兼容,因为BroadcastReceiver没有打印任何RSSI.

Could it be that the Samsung Gear Live is incompatible with RSSI services, because the BroadcastReceiver is not printing any RSSIs.

非常感谢, LHJ

推荐答案

Android Wear的蓝牙通信由Android操作系统处理.由于不存在蓝牙适配器,因此建立连接后就无法通过应用程序访问RSSI数据.

Bluetooth communication of the Android Wear is handled by the Android Operating system. Since no Bluetooth adapter is present, there is no access to RSSI data through an application after a connection has been established.

这篇关于在Android手机上获取Android Wear的蓝牙RSSI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆