android之蓝牙操作(一)
与蓝牙相关的API
1、BluetoothAdapter
该类的对象代表了本地的蓝牙适配器
2、BluetoothDevice
该类的对象代表了远程的蓝牙适配器
扫描已经配对的蓝牙设备步骤:
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("本机上无蓝牙设备"); } } } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28