android 蓝牙 列表,android蓝牙列表配对设备的例子

BluetoothAdapter类的getBoundedDevices()方法提供了一个包含所有配对或绑定的蓝牙设备的列表的集合。

在此示例中,我们正在检查蓝牙是否已关闭,如果是,则将其打开并列出所有配对的设备。

activity_main.xml

从面板上拖动一个textview,现在activity_main.xml文件将如下所示:

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

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginLeft="18dp"

android:layout_marginTop="61dp"

android:text="Showing Paired Devices:" />

提供权限

你需要在AndroidManifest.xml文件中提供以下权限。

AndroidManifest.xml文件的完整代码如下。

package="com.example.bluetoothshowpaired"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="17" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.example.bluetoothshowpaired.MainActivity"

android:label="@string/app_name" >

活动类

让我们编写代码以提供配对(有界)蓝牙设备的列表。

package com.example.bluetoothshowpaired;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import java.util.Set;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.content.Intent;

import android.widget.TextView;

public class MainActivity extends Activity {

TextView textview1;

private static final int REQUEST_ENABLE_BT = 1;

BluetoothAdapter btAdapter;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textview1 = (TextView) findViewById(R.id.textView1);

// Getting the Bluetooth adapter

btAdapter = BluetoothAdapter.getDefaultAdapter();

textview1.append("\nAdapter: " + btAdapter);

CheckBluetoothState();

}

/* It is called when an activity completes.*/

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_ENABLE_BT) {

CheckBluetoothState();

}

}

@Override

protected void onDestroy() {

super.onDestroy();

}

private void CheckBluetoothState() {

// Checks for the Bluetooth support and then makes sure it is turned on

// If it isn't turned on, request to turn it on

// List paired devices

if(btAdapter==null) {

textview1.append("\nBluetooth NOT supported. Aborting.");

return;

} else {

if (btAdapter.isEnabled()) {

textview1.append("\nBluetooth is enabled...");

// Listing paired devices

textview1.append("\nPaired Devices are:");

Set devices = btAdapter.getBondedDevices();

for (BluetoothDevice device : devices) {

textview1.append("\n Device: " + device.getName() + ", " + device);

}

} else {

//Prompt user to turn on Bluetooth

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

}

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

}

你需要在真实设备(例如移动设备)上运行它以测试应用程序。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值