android 蓝牙通讯实现手机蓝牙的开启,并扫描附近可见的蓝牙设备
2015-04-09 14:23
661 查看
蓝牙是一种重要的短距离无线通信协议,广泛应用于各种设备(手机,医疗,汽车等)。蓝牙是比较常用的无线通信设备,早研究成为手机的标配。现在的安卓手机基本上都有蓝牙,所有通过蓝牙对数据有很好的硬件基础
在Android中,与蓝牙有关的类和接口在android.bluetooth包中。其中BluetoothAdapter是蓝牙中的核心类,代表本地的蓝牙适配器设备。BluetoothAdapter类让用户能执行基本的蓝牙任务。例如: 初始化设备的搜索,查询可匹配的设备集,使用一个已知的MAC地址来初始化一个BluetoothDevice类,创建一个
BluetoothServerSocket类以监听其它设备对本机的连接请求等。
打开和扫描蓝牙,并将扫面到的蓝牙设备显示在listview上
BluetoothActivity
在Android中,与蓝牙有关的类和接口在android.bluetooth包中。其中BluetoothAdapter是蓝牙中的核心类,代表本地的蓝牙适配器设备。BluetoothAdapter类让用户能执行基本的蓝牙任务。例如: 初始化设备的搜索,查询可匹配的设备集,使用一个已知的MAC地址来初始化一个BluetoothDevice类,创建一个
BluetoothServerSocket类以监听其它设备对本机的连接请求等。
打开和扫描蓝牙,并将扫面到的蓝牙设备显示在listview上
BluetoothActivity
package cn.edu.cqu.bluetooth; import java.util.ArrayList; import java.util.Iterator; import java.util.Set; import com.example.project.R; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class BluetoothActivity extends Activity{ private Button button2; private ListView list; private ArrayAdapter mArrayAdapter; private BluetoothReceiver bluetoothReceiver; private BluetoothAdapter bluetoothAdapter; private BluetoothDevice device; private ArrayList<String> PairedMaclist; private TextView textView1; int flag; @Override protected void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.bluetooth); list = (ListView)findViewById(R.id.listView); textView1 = (TextView)findViewById(R.id.textView); //创建一个IntentFilter对象,将其action指定为BluetoothDevice.ACTION_FOUND,查找蓝牙 IntentFilter intentFileter = new IntentFilter(BluetoothDevice.ACTION_FOUND); bluetoothReceiver = new BluetoothReceiver(); //注册广播接收器 registerReceiver(bluetoothReceiver, intentFileter); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); button2 = (Button)findViewById(R.id.scanButton); button2.setOnClickListener(new ScanButtonListener()); list.setOnItemClickListener(new ListViewListener()); openBluetooth(); mArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1); PairedMaclist=new ArrayList<String>(); } @Override protected void onDestroy() { unregisterReceiver(bluetoothReceiver); super.onDestroy(); } private void openBluetooth() { //bluetoothAdapter.enable(); if(bluetoothAdapter != null){ if(!bluetoothAdapter.isEnabled()){ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent); } //得到所有已经被对的蓝牙适配器对象 Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices(); if(devices.size() > 0){ for(Iterator<BluetoothDevice> iterator = devices.iterator();iterator.hasNext();){ BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next(); //得到远程蓝牙设备的地址 System.out.println(bluetoothDevice.getAddress()); } } } else { //System.out.println("没有蓝牙设备"); Toast.makeText(BluetoothActivity.this, "没有蓝牙设备",0).show(); } } class BluetoothReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)){ //得到intent里面的信息 device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Toast.makeText(BluetoothActivity.this, "发现蓝牙设备",0).show(); //System.out.println(device.getAddress()); mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); PairedMaclist.add(device.getAddress()); list.setAdapter(mArrayAdapter); } } } class ScanButtonListener implements OnClickListener{ @Override public void onClick(View arg0) { mArrayAdapter.clear(); bluetoothAdapter.startDiscovery(); Toast.makeText(BluetoothActivity.this, "开始扫描",0).show(); } } class ListViewListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long id) { String str = PairedMaclist.get(position); //System.out.println("Str-------------" + str); Intent intent = new Intent(BluetoothActivity.this,BluetoothWaveform.class); intent.putExtra("BluetoothMAC",str); startActivity(intent); } } }
显示和扫描蓝牙布局文件
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:orientation="vertical" > <Button android:id="@+id/scanButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:text="扫描蓝牙设备"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:textSize="20sp" android:text="搜索到以下设备:"/> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout><img src="http://img.blog.csdn.net/20150409144408261?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmZlbmdkZWp1YW5saWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
相关文章推荐
- android之蓝牙通信(一)(检测蓝牙、开启蓝牙、扫描附近蓝牙设备)
- Android 扫描附近的蓝牙设备并连接蓝牙音响的示例
- Android BLE最完整的工具类(扫描/连接/读写/通知设备),可以手机与设备进行通讯
- Android 扫描附近的蓝牙设备并连接蓝牙音响
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
- ANDROID_MARS学习笔记_S03_002_设置可见性及扫描蓝牙设备
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- android 手机与蓝牙设备的通讯
- Android开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
- Android 扫描附近的蓝牙设备并连接蓝牙音响的示例
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备(转)
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
- Android 蓝牙设备的开启与关闭功能的实现
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- 蓝牙开启&& 被附近的其它蓝牙设备发现
- 如何从后台开启android蓝牙的可见性以及始终保持可见性
- Android通过JNI实现与C语言的串口通讯操作蓝牙硬件模块
- Android开发之蓝牙--扫描已经配对的蓝牙设备
- 如何从后台开启android蓝牙的可见性以及始终保持可见性
- Android 扫描周围蓝牙设备