获取 Android 的蓝牙 RSSI Wear on Android phone

Get Bluetooth RSSI of Android Wear on an Android phone

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

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

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

但是,使用AndroidWear时,不需要使用蓝牙管理器、适配器或GATT服务;我没有看到它们在可穿戴示例项目中被初始化。这是我的问题:如何在配对后以大约 5 秒的间隔从 phone 上的 Android Wear 获取蓝牙 RSSI 值。有人可以指出大致按照我计划执行的示例代码吗?

[编辑] 我找到了许多代码示例并以以下 MainActivity 结尾。我能够获得配对设备的列表,然后我开始发现并使用 BroadcastReceiver 尝试读出 RSSI 值。

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。

非常感谢, LHJ

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