android 蓝牙通讯实现手机蓝牙的开启,并扫描附近可见的蓝牙设备
生活随笔
收集整理的這篇文章主要介紹了
android 蓝牙通讯实现手机蓝牙的开启,并扫描附近可见的蓝牙设备
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
藍牙是一種重要的短距離無線通信協議,廣泛應用于各種設備(手機,醫療,汽車等)。藍牙是比較常用的無線通信設備,早研究成為手機的標配。現在的安卓手機基本上都有藍牙,所有通過藍牙對數據有很好的硬件基礎
在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;@Overrideprotected 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>();}@Overrideprotected 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{@Overridepublic 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{@Overridepublic void onClick(View arg0) {mArrayAdapter.clear();bluetoothAdapter.startDiscovery();Toast.makeText(BluetoothActivity.this, "開始掃描",0).show();}}class ListViewListener implements OnItemClickListener{@Overridepublic 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="https://img-blog.csdn.net/20150409144408261?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmZlbmdkZWp1YW5saWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />總結
以上是生活随笔為你收集整理的android 蓝牙通讯实现手机蓝牙的开启,并扫描附近可见的蓝牙设备的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 收银机房间怎么调整位置?
- 下一篇: android 调用本地第三方应用软件,