android蓝牙在有效范围内自动连接,android – 如何在范围内找到可用的蓝牙设备?...

这就是我在Activity中搜索蓝牙设备并在ListView中显示其名称和mac地址的方法.除了在ListView中显示设备外,您几乎可以使用发现的BluetoothDevice对象执行任何操作.

FindBluetoothActivity.java

public class FindBluetoothActivity extends Activity {

private BluetoothAdapter mBtAdapter;

private ListView mLvDevices;

private ArrayList mDeviceList = new ArrayList();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_find_bluetooth);

mLvDevices = (ListView) findViewById(R.id.lvDevices);

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

registerReceiver(mBtReceiver, filter);

// Getting the Bluetooth adapter

mBtAdapter = BluetoothAdapter.getDefaultAdapter();

if(mBtAdapter != null) {

mBtAdapter.startDiscovery();

Toast.makeText(this, "Starting discovery...", Toast.LENGTH_SHORT).show();

}

else {

Toast.makeText(this, "Bluetooth disabled or not available.", Toast.LENGTH_SHORT).show();

}

}

@Override

protected void onDestroy() {

super.onDestroy();

if (mBtAdapter != null) {

mBtAdapter.cancelDiscovery();

}

unregisterReceiver(mBtReceiver);

}

private final BroadcastReceiver mBtReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

mDeviceList.add(device.getAddress() + ", " + device.getName()); // get mac address

ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, mDeviceList);

mLvDevices.setAdapter(adapter);

}

}

};

}

布局.xml文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".FindBluetoothActivity" >

android:id="@+id/lvDevices"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true" >

Android Manifest.xml文件:

package="com.example.bluetoothexample"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="11"

android:targetSdkVersion="18" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@android:style/Theme.Holo.Light" >

android:name="com.example.bluetoothexample.FindBluetoothActivity"

android:label="@string/app_name" >

附加信息:

>确保您的设备已启用蓝牙>将权限android.permission.BLUETOOTH和android.permission.BLUETOOTH_ADMIN添加到您的Manifest.xml文件>确保在销毁活动时取消注册广播接收器>请记住,您的应用程序需要找到范围内的蓝牙设备.仅在它们上启用蓝牙可能还不够.通常情况是,在其他设备可以发现设备之前,用户需要启用某种“可发现”模式.>请注意,通过蓝牙可以发现设备的范围通常在室内大约10米,室外大约50米

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值