2014-03-06 377 views
1

我列出了所有配對的設備,它很好地工作,但現在想要獲得配對設備的藍牙信號強度......我知道它會通過使用rssi得到,但無法實現得到它不斷在我的應用程序.. PLZ我通過給予適當的代碼作爲我的代碼...我的代碼是在這裏...Android持續獲得配對設備的藍牙信號強度

public class Security extends Fragment implements OnClickListener{ 
    private BluetoothAdapter BA; 
    private Set<BluetoothDevice>pairedDevices; 

    ArrayList<String> mylist = new ArrayList<String>(); 

    //private Object ImageView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.security, null); 

    BA = BluetoothAdapter.getDefaultAdapter(); 

    /* starting the bluetooth*/ 
    on(v); 
    pairedDevices = BA.getBondedDevices(); 
    //length=4; 
    // int j=1; 

    for(BluetoothDevice bt : pairedDevices) { 
     mylist.add(bt.getName()); 
     length=j; 
     j++; 

     bt.getBondState(); 
    } 

    return v; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    // Toast.makeText(getActivity(), "On resume", Toast.LENGTH_LONG).show(); 
    } 

    /*************************Bluetooth function****************************/ 

     public void on(View view){ 
      if (!BA.isEnabled()) { 
      Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(turnOn, 0); 
      Toast.makeText(getActivity(),"Turned on" 
      ,Toast.LENGTH_LONG).show(); 
      } else{ 
      // Toast.makeText(getActivity(),"Already on", 
      // Toast.LENGTH_LONG).show(); 
      } 
     } 

     public void Discovery(View view) { 
      if(BA.isDiscovering()) { 
      BA.cancelDiscovery(); 
      } 
     } 

     public void list(View view){ 
       Toast.makeText(getActivity(),"Showing Paired Devices", 
       Toast.LENGTH_SHORT).show(); 
      } 

    @Override 
    public void onClick(View v) { 
     for(int j=0;j<length;j++) { 
     if(v.getId()==j) 
     Toast.makeText(getActivity(), mylist.get(j), Toast.LENGTH_LONG).show(); 
     //hand.update(run,1000); 
     } 

    } 

} 
+0

你想只用於配對設備嗎?因爲你沒有發現藍牙設備。 – SilentKiller

+0

是隻有主動配對設備... – Dhiman

+0

如果您正在考慮主動設備,那麼它將已與您的設備配對。 – SilentKiller

回答

2

您可以從下面的代碼得到信號。當你的設備將被連接到遠程設備

代碼的活動

@Override 
public void onCreate(Bundle savedInstanceState) { 
    ..... 
    // Registering Broadcast. this will fire when Bluetoothdevice Found 
    registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED)); 
} 

private final BroadcastReceiver BroadcastReceiver = new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String mIntentAction = intent.getAction(); 
     if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(mIntentAction)) { 
      int RSSI = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 
      String mDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); 
     } 
    } 
}; 

此廣播將被執行。 還有其他幾個可以用來播放此廣播的操作。看看here(BluetoothDevice)

檢查下面的鏈接,RSSI的恆念

Tutorial to continuously measure the Bluetooth RSSI of a connected Android device (Java)

鏈接的輸出:

enter image description here

+0

我知道這一點,但它在運行應用程序中檢索rssi一次...我想要在運行應用程序的時間片之後不斷地... – Dhiman

+0

然後你需要不斷地調用它。意思是在TimerTask – SilentKiller

+0

如何??????? – Dhiman

1

然後,如果你想這樣做在持續,你需要在服務或線程中運行它。這樣你甚至可以添加時隙或者測量RSSI的睡眠(等待)。