android之蓝牙操作(一)

[b]与蓝牙相关的API [/b]

1、BluetoothAdapter
该类的对象代表了本地的蓝牙适配器
2、BluetoothDevice
该类的对象代表了远程的蓝牙适配器

[b]扫描已经配对的蓝牙设备步骤:[/b]

1、获得BluetoothAdapter对象
2、判断当前的设备中是否有蓝牙设备
3、判断当前设备中的蓝牙设备是否已经打开
4、得到所以已经配对的蓝牙设备对象

在AndroidManifedt.xml中声明蓝牙权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.bluetooth01"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestBluetoothActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

<uses-permission android:name="android.permission.BLUETOOTH"/>
</manifest>


在布局文件中添加一个按钮
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="扫描蓝牙设备"
/>
</LinearLayout>



MainActivity.java

import java.util.Iterator;
import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestBluetoothActivity extends Activity {
/** Called when the activity is first created. */
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonListener());
}
private class ButtonListener implements OnClickListener
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//得到BluetoothAdapter对象
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//判断BluetoothAdapter对象是否为空,若为空,则本机上无蓝牙设备
if (bluetoothAdapter != null) {
System.out.println("本机上拥有蓝牙设备");
if (!bluetoothAdapter.enable()) {
//创建一个Intent对象,该对象用来启动另外一个Activity,提示用户启动蓝牙设备
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
//得到所有已经匹配的蓝牙适配器对象
Set<BluetoothDevice> device = bluetoothAdapter.getBondedDevices();
if (device.size()>0) {
for (Iterator iterator = device.iterator(); iterator
.hasNext();) {
BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator
.next();
System.out.println(bluetoothDevice.getAddress());
}
}

}
else
{
System.out.println("本机上无蓝牙设备");
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值