Android-开启蓝牙
来源:互联网 发布:java中md5加密怎么写 编辑:程序博客网 时间:2024/04/30 09:20
蓝牙操作
什么是蓝牙?(5—10m,受障碍影响大,分民用和军用)
发现周围的蓝牙设备?
与蓝牙相关的API
1.BluetoothAdapter该类的对象代表了本地的蓝牙适配器
2.BluetoothDevice代表了一个远程的Bluetooth设备
扫描已经配对的蓝牙设备:
1.蓝牙操作必须运行在手机上,模拟器上不能模拟蓝牙
2.暂时无法通过代码让我们的手机与其他设备进行配对,只能通过手动实现!
首先,在androidManifest.xml文件中声明蓝牙的权限:
<uses-permission
android:name = "android.permission.BLUETOOTH" />
四个步骤实现对已配对的蓝牙设备的通信:
1.获得BluetoothAdapter对象;
2.判断当前设备中是否拥有蓝牙设备;
3.判断当前设备中的蓝牙设备是否已经打开;
strings.xml和layout.xml中比较简单,如下:
manifest.xml代码中注意权限设置的代码:
什么是蓝牙?(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{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSystem.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());}}}elseSystem.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>
- Android 关闭开启蓝牙
- Android 关闭开启蓝牙
- Android-开启蓝牙
- android 开启蓝牙
- android 监听蓝牙开启关闭
- Android蓝牙的开启-搜索-关闭演示
- Android 监测蓝牙程序是否开启
- Android 虚拟机判断蓝牙是否开启
- 开启蓝牙
- android之蓝牙通信(一)(检测蓝牙、开启蓝牙、扫描附近蓝牙设备)
- android 蓝牙通讯实现手机蓝牙的开启,并扫描附近可见的蓝牙设备
- Android开发蓝牙篇之蓝牙设备开启、搜索周边蓝牙设备
- android蓝牙从开启到关闭的正常状态转变
- android 事件监听,蓝牙开启与关闭,java_Date日期
- Android BlueDroid(二):BlueDroid蓝牙开启过程init
- Android BlueDroid(三):BlueDroid蓝牙开启过程enable
- Android BlueDroid(二):BlueDroid蓝牙开启过程init
- Android BlueDroid(三):BlueDroid蓝牙开启过程enable
- Linux编程练习(一)—— 多线程+共享内存+线程信号量练习
- 输出某个数的反序数
- 在OnCtreate方法或者adapter中获取控件的宽高和位置
- 北大ACM试题分类 - 实时更新我所有的解题报告链接
- 数据结构-双向链表
- Android-开启蓝牙
- ExtJs中decode与encode
- Lisp语言发明者 人工智能奠基人 John McCarthy
- OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows,
- HDU4772--Zhuge Liang's Password(160)
- android 测试分享图片+文字到微博
- UNIX / Linux: 10 Netstat Command Examples
- Ant的实现原理
- android游戏物理引擎开发——重力引擎(一)