很纳闷这个人怎么火起来的

4067人阅读
Android 蓝牙学习
要拿到蓝牙信号指示值 rssi 分为两个步骤。&
1.在oncreate方法里面增加 注册扫描广播&
public void onCreate(Bundle savedInstanceState) {&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& // 注册开始发现广播。&
&&&&&&&&&&&&&&&&&&&&&&&&&&&& IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);&
&&&&&&&&&&&&&&&&&&&&&&&&&&&& this.registerReceiver(mReceiver, filter);&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&& }&
2.新建BroadcastReceiver广播对象,并实现里面的onreceive方法,在onreceive得到rssi(信号强度)。&
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {&
&&&&&&&&&&&&&&& @Override&
&&&&&&&&&&&&&&& public void onReceive(Context context, Intent intent) {&
&&&&&&&&&&&&&&&&&&&&&&& String action = intent.getAction();&
&&&&&&&&&&&&&&&&&&&&&&& //当设备开始扫描时。&
&&&&&&&&&&&&&&&&&&&&&&& if (BluetoothDevice.ACTION_FOUND.equals(action)) {&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //从Intent得到blueDevice对象&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& BluetoothDevice device = intent&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& if (device.getBondState() != BluetoothDevice.BOND_BONDED) {&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //信号强度。&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& short rssi = intent.getExtras().getShort(&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& BluetoothDevice.EXTRA_RSSI);&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&& }&&
&&&&&&&&&&&&&&& }&
&&&&&&& };&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
(1)(4)(5)(1)(10)(4)(5)(5)(7)(5)(13)(6)(14)(4)(13)新浪广告共享计划>
广告共享计划
安卓程序:&获取手机的信号强度
1 & 新建一个安卓程序
新项目的Andr​​oid Android项目&
项目名称: Test
选择的Andr​​oid 2.2
包名: &com.unikran.GetGsmSignalStrength
创建Activity:GetGsmSignalStrength
修改清单文件,内容如下
&?xml version="1.0"
encoding="utf-8"?&
xmlns:android="/apk/res/android"
package="com.unikran.GetGsmSignalStrength"
android:versionCode="1"
android:versionName="1.0"&
&uses-sdk android:minSdkVersion="8"
&uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
&uses-permission
android:name="android.permission.READ_PHONE_STATE"
&&uses-permission
android:name="android.permission.CHANGE_NETWORK_STATE"
&application android:icon="@drawable/icon"
android:label="@string/app_name"&
& &activity
android:name=".GetGsmSignalStrength"
android:label="@string/app_name"&
&intent-filter&
& & &action
android:name="android.intent.action.MAIN" /&
& android:name="android.intent.category.LAUNCHER"
&/intent-filter&
& &/activity&
&/application&
&/manifest&
增加一个 &。。。
&&\res\drawable 目录,
并将一个icon.png图片文件放在这个目录下
主程序文件内容如下:
package com.unikran.GetGsmSignalS
import android.app.A
import android.os.B
import android.content.C
import android.telephony.PhoneStateL
import android.telephony.SignalS
import android.telephony.TelephonyM
import android.widget.T
public class GetGsmSignalStrength extends Activity
& &TelephonyManager
& &MyPhoneStateListener
& & @Override
& & public void
onCreate(Bundle savedInstanceState)
& super.onCreate(savedInstanceState);
& setContentView(R.layout.main);
& MyListener & = new
MyPhoneStateListener();
& = ( TelephonyManager
)getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener
,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
& & @Override
& &protected void
super.onPause();
Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
& &@Override
& &protected void
onResume()
super.onResume();
Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
& & private class
MyPhoneStateListener extends PhoneStateListener
public void onSignalStrengthsChanged(SignalStrength
signalStrength)
&super.onSignalStrengthsChanged(signalStrength);
&Toast.makeText(getApplicationContext(), "Go to
Firstdroid!!! GSM Cinr = "
String.valueOf(signalStrength.getGsmSignalStrength()),
Toast.LENGTH_SHORT).show();
在我的手机上运行通过。&
但是在WCDMA状态下, 它显示一个值。&
& & 在GSM状态下,
它也显示一个值。&
& & 这是怎么回事呢?
int&getGsmSignalStrength&()
Get the GSM Signal Strength, valid values are (0-31, 99) as defined
in TS 27.007 8.5
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。[android]如何读取一次,我连接的多个设备的 rssi 值?
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
我正在开发一个应用程序有连接到蓝牙设备的 Android 4.3。
我可以连接到 BLE 设备和从设备中读取 RSSI,通过使用 BluetoothGatt.readRemoteRssi()。
我想要读的多个设备一次我已连接,但我可以只读 BLE RSSI 设备 RSSI 的最后一次,我连接的设备。
如果有两个 BLE 设备 A 和 B。我连接到A 的设备,并读取该 RSSI。之后,我连接到B 的设备,和我可以从设备 B读取 RSSI。但它并不读取设备 A的 RSSI,它只能从设备 B读取 RSSI。
在Main.java ,它列出已连接的位置的所有设备。
当我单击该设备,在列表上,它把传送的设备的名称和地址到DeviceControl.java。
final Intent qintent = new Intent(this, DeviceControl.class);
devicelist.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView&?& arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
HashMap&String, Object& select = (HashMap&String, Object&) devicelist.getItemAtPosition(arg2);
String name = (String) select.get("name");
String address = (String) select.get("address");
qintent.putExtra(DeviceControl.EXTRAS_DEVICE_NAME, name);
qintent.putExtra(DeviceControl.EXTRAS_DEVICE_ADDRESS, address);
startActivity(qintent);
DeviceControl.java将调用BluetoothLeService.java和连接到设备。
private final ServiceConnection mServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName componentName, IBinder service) {
// TODO Auto-generated method stub
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if(!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
mBluetoothLeService.connect(mDeviceAddress);
public void onServiceDisconnected(ComponentName componentName) {
// TODO Auto-generated method stub
mBluetoothLeService =
BluetoothLeService.java将连接到该设备。
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
if(mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
&& mBluetoothGatt != null) {
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if(mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if(device == null) {
Log.w(TAG, "Device not found.
Unable to connect");
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.d(TAG, "Try to create a new connection");
mBluetoothDeviceAddress =
mConnectionState =STATE_CONNECTING;
它连接到设备后,我可以使用 readRemoteRssi 来从设备中读取 RSSI。
public void readRemoteRssi() {
mBluetoothGatt.readRemoteRssi();
但它只能读取 RSSI 的最后一个设备的我已连接。
当我看到日志时,它总是发送onCharacteristicWrite和readRemoteRssi()到我连接的最后一个设备。
我应该重新连接关贸总协定 》 或之前我想 RSSI 对读取或写入的 CharacteristicWrite 值的第一个设备重新连接到的第一个地址的设备概览
它不会有其他的方法来读取我已连接的所有设备的 RSSI 概览
解决方法 1:
使多个 BluetoothGatt 对象来连接多个设备分开,并调用 readRemoteRssi 一个接一个。
懒的和坏的示例中,您应该能够将那些 BluetoothGatt 对象放入数组
BluetoothGatt mBluetoothGatt1 = device1.connectGatt(this, false, mGattCallback);
BluetoothGatt mBluetoothGatt2 = device2.connectGatt(this, false, mGattCallback);

我要回帖

 

随机推荐