android蓝牙测试
来源:互联网 发布:数学辅导书初中淘宝 编辑:程序博客网 时间:2024/06/13 09:50
1.添加权限
<uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2.main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.bluetoothtest.MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></RelativeLayout>
3.MainActivity.java
package com.example.bluetoothtest;import java.util.ArrayList;import java.util.List;import java.util.Set;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.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends Activity { private BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); private List<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>(); private int REQUEST_ENABLE_BT = 1; private ListView listView; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView)findViewById(R.id.listView1); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); listView.setAdapter(adapter); if (bluetoothAdapter == null) { Toast.makeText(getApplicationContext(), "bluetooth not exist!", Toast.LENGTH_SHORT).show(); finish(); } else if (!bluetoothAdapter.isEnabled()) { // Show a system activity that allows the user to turn on Bluetooth Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, REQUEST_ENABLE_BT); } } @Override protected void onStart() { super.onStart(); // 注册接收器 Intent discoverIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); // 开启搜索看见模式,最长10s discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 10); startActivity(discoverIntent); IntentFilter discoverFilter = new IntentFilter( BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(discover_R, discoverFilter); IntentFilter findFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(find_R, findFilter); bluetoothAdapter.startDiscovery(); } @Override protected void onDestroy() { super.onDestroy(); if (bluetoothAdapter != null) { bluetoothAdapter.cancelDiscovery(); bluetoothAdapter.disable(); } } // ACTION_DISCOVERY_FINISHED,搜索结束后追加已配对的设备 // getName()这一步比较漫长,“已配对”那句话要等好一会儿才出现 BroadcastReceiver discover_R = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { getName(); unregisterReceiver(find_R); unregisterReceiver(this); } }; // ACTION_FOUND,这是搜索到的所有蓝牙(未配对+已配对) BroadcastReceiver find_R = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); deviceList.add(device); adapter.add(device.getName()); } }; private void getName() { Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); if(pairedDevices.size() > 0){ for(BluetoothDevice device : pairedDevices){ deviceList.add(device); adapter.add(device.getName()+"已配对!"); } } }}
注意点:在执行getBondedDevices时候会花费一点时间,一开始我不知道,“已配对”一直没出现,以为出错了。后来上了趟厕所回来发现就有了!!!!尴尬!
0 0
- android蓝牙测试
- Android编程9:蓝牙测试
- Android开发教程:蓝牙测试
- android 蓝牙
- Android 蓝牙
- android 蓝牙
- Android蓝牙
- Android 蓝牙
- Android 蓝牙
- Android蓝牙
- Android蓝牙
- Android 蓝牙
- Android蓝牙
- android-蓝牙
- Android 蓝牙
- Android-蓝牙
- Android 蓝牙
- Android蓝牙
- 【BZOJ4500】矩阵 DFS
- Android集成Fresco框架导致在64位机器上(三星S6,华为P8等)找不到其他so库
- RESTEasy:JAX-RS restful webservices 示例
- 写连接代码时需要注意2000和2005的不同:
- code vs 1090 加分二叉树 (树形DP)
- android蓝牙测试
- Linux 基础知识(二)
- LeetCode *** 344. Reverse String
- 单源最短路径 --Dijkstra算法-通过边实现松弛
- JAVA_HOME的小问题
- nyoj 45棋盘覆盖
- hdoj2008 (java)数值统计
- Tomcat for windows, Chrome for ubuntu
- hrbust/哈理工oj 2116 Maximum continuous product【水题】