2016-02-14 105 views
0

我想通過藍牙RSSI確定Android手機(Nexus 5X)和Android Wear設備(Samsung Gear live)之間的距離。在Android手機上獲取Android Wear的藍牙RSSI

我找到解決方案,以使用藍牙功能readRemoteRssi();

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

然而,在使用Android Wear的時候,沒有必要使用藍牙管理器,適配器或GATT服務;我沒有看到他們在可穿戴示例項目中初始化。這裏是我的問題:在配對之後,我如何從手機上的Android Wear獲取藍牙RSSI值,間隔時間爲5秒。有人可以指點我的示例代碼,大概是我打算做的事情嗎?

[編輯]我找到了一些代碼示例,並以下面的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); 
      } 
     } 
    }; 

} 

難道是三星Gear Live與RSSI服務不兼容,因爲廣播接收器沒有打印任何的RSSI。

非常感謝, LHJ

+0

給出您提到的示例可穿戴項目的鏈接 – Neo

+0

理想情況下,我想在此SensorDashboard應用程序中實現此功能https://github.com/pocmo/SensorDashboard – LHJ

回答

0

的Android Wear的藍牙通信是通過Android操作系統處理。由於不存在藍牙適配器,因此建立連接後不能通過應用程序訪問RSSI數據。