蓝牙操作
什么是蓝牙?(5—10m,受障碍影响大,分民用和军用)
发现周围的蓝牙设备?
与蓝牙相关的API
1.BluetoothAdapter该类的对象代表了本地的蓝牙适配器
2.BluetoothDevice代表了一个远程的Bluetooth设备
扫描已经配对的蓝牙设备:
1.蓝牙操作必须运行在手机上,模拟器上不能模拟蓝牙
2.暂时无法通过代码让我们的手机与其他设备进行配对,只能通过手动实现!
首先,在androidManifest.xml文件中声明蓝牙的权限:
<uses-permission
android:name = "android.permission.BLUETOOTH" />
四个步骤实现对已配对的蓝牙设备的通信:
1.获得BluetoothAdapter对象;
2.判断当前设备中是否拥有蓝牙设备;
3.判断当前设备中的蓝牙设备是否已经打开;
4.得到所有已经配对的蓝牙设备对象
代码如下:
主activity代码:
package thu.wolf.bluetooth; 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.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Bluetooth_MainActivity extends Activity { private Button btn = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bluetooth__main); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new btnOnClickListener()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.bluetooth__main, menu); return true; } //获取bluetoothAdapter适配器 class btnOnClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub System.out.println("wait for bluetooth."); //获取bluetoothadapter对象 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if( adapter != null)//判断是否为空 { System.out.println("本机拥有蓝牙设备。"); if(!adapter.isEnabled())//判断当前蓝牙设备是否可用 { System.out.println("什么?"); //创建一个Intent对象,用于启动一个activity,来提示用户开启蓝牙 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent); } System.out.println("here i am!"); //得到所以已经配对的蓝牙适配器对象 Set<BluetoothDevice> devices = adapter.getBondedDevices(); if(devices.size() >0 ) { for(Iterator iterator = devices.iterator(); iterator.hasNext(); ) { BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next(); System.out.println(bluetoothDevice.getAddress()); } } } else System.out.println("没有蓝牙设备!"); } } }
strings.xml和layout.xml中比较简单,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 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" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/connection" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">bluetooth</string> <string name="action_settings">Settings</string> <string name="connection">打开蓝牙设备</string> </resources>
manifest.xml代码中注意权限设置的代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="thu.wolf.bluetooth" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="thu.wolf.bluetooth.Bluetooth_MainActivity" 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>