Android编程之蓝牙测试实例

本文实例讲述了Android编程之蓝牙测试。分享给大家供大家参考。具体分析如下:

一、软件平台:

win7 + eclipse + sdk

二、设计思路:

配合倒计时定时器实现蓝牙打开,可见,扫描三个功能

三、源代码:

main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content"></TextView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1"> <Button android:id="@+id/button1" android:text="OFF" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2"> <Button android:id="@+id/button2" android:text="开启可见 " android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设备不可见 "></TextView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3"> <Button android:id="@+id/button3" android:text="扫描:OFF" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止扫描 "></TextView> </LinearLayout> <ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="match_parent"></ListView> </LinearLayout>

test_bluetooth.java:

package com.test_bluetooth; 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.os.CountDownTimer; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; public class test_bluetooth extends Activity implements View.OnClickListener { private static final int REQUEST_ENABLE_BT = 2; TextView txt; TextView txt_see; TextView txt_scan; BluetoothAdapter mBluetoothAdapter; ArrayAdapter<String> mArrayAdapter; Button btn_switch; Button btn_see; Button btn_scan; ListView list; CountDownTimer see_timer; CountDownTimer scan_timer; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt = (TextView)findViewById(R.id.textView1); txt_see = (TextView)findViewById(R.id.textView2); txt_scan = (TextView)findViewById(R.id.textView3); //绑定XML中的ListView,作为Item的容器 list = (ListView) findViewById(R.id.listView1); //获取蓝牙适配器 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); if (mBluetoothAdapter == null) { // Device does not support Bluetooth txt.setText("fail"); //退出程序 test_bluetooth.this.finish(); } btn_switch = (Button)findViewById(R.id.button1); btn_switch.setOnClickListener(this); btn_see = (Button)findViewById(R.id.button2); btn_see.setOnClickListener(this); btn_see.setEnabled(false); btn_scan = (Button)findViewById(R.id.button3); btn_scan.setOnClickListener(this); btn_scan.setText("扫描:OFF"); btn_scan.setEnabled(false); //判断蓝牙是否已经被打开 if (mBluetoothAdapter.isEnabled()) { //打开 btn_switch.setText("ON"); btn_see.setEnabled(true); btn_scan.setEnabled(true); } see_timer = new CountDownTimer(120000,1000) { @Override public void onTick( long millisUntilFinished) { txt_see.setText( "剩余可见时间" + millisUntilFinished / 1000 + "秒"); } @Override public void onFinish() { //判断蓝牙是否已经被打开 if (mBluetoothAdapter.isEnabled()) { btn_see.setEnabled(true); txt_see.setText( "设备不可见"); } } }; scan_timer = new CountDownTimer(12000,1000) { @Override public void onTick( long millisUntilFinished) { txt_scan.setText( "剩余扫描时间" + millisUntilFinished / 1000 + "秒"); } @Override public void onFinish() { //判断蓝牙是否已经被打开 if (mBluetoothAdapter.isEnabled()) { btn_scan.setEnabled(true); //关闭扫描 mBluetoothAdapter.cancelDiscovery(); btn_scan.setText("扫描:OFF"); txt_scan.setText( "停止扫描"); } } }; } @Override protected void onDestroy() { super.onDestroy(); android.os.Process.killProcess(android.os.Process.myPid()); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: { String str = btn_switch.getText().toString(); if (str == "OFF") { if (!mBluetoothAdapter.isEnabled()) { //打开蓝牙 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); txt.setText("s1"); btn_see.setEnabled(true); btn_scan.setText("扫描:OFF"); btn_scan.setEnabled(true); } } else { //关闭蓝牙 mBluetoothAdapter.disable(); btn_switch.setText("OFF"); mArrayAdapter.clear(); list.setAdapter(mArrayAdapter); btn_see.setEnabled(false); btn_scan.setEnabled(false); } break; } case R.id.button2: { //开启可见 Intent enableBtIntent_See = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(enableBtIntent_See, 3); txt.setText("s1"); btn_see.setEnabled(false); see_timer.start(); break; } case R.id.button3: { String str = btn_scan.getText().toString(); if (str == "扫描:OFF") { txt.setText("s5"); if (mBluetoothAdapter.isEnabled()) { //开始扫描 mBluetoothAdapter.startDiscovery(); txt.setText("s6"); btn_scan.setText("扫描:ON"); // Create a BroadcastReceiver for ACTION_FOUND final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); // When discovery finds a device mArrayAdapter.clear(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView mArrayAdapter.add(device.getName() + ":" + device.getAddress()); } list.setAdapter(mArrayAdapter); } }; // Register the BroadcastReceiver IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy scan_timer.start(); } } else { //关闭扫描 mBluetoothAdapter.cancelDiscovery(); btn_scan.setText("扫描:OFF"); scan_timer.cancel(); txt_scan.setText( "停止扫描"); } break; } default: break; } } public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_ENABLE_BT: // When the request to enable Bluetooth returns if (resultCode == Activity.RESULT_OK) { // Bluetooth is now enabled, so set up a chat session btn_switch.setText("ON"); txt.setText("s4"); //获取蓝牙列表 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); mArrayAdapter.clear(); // If there are paired devices if (pairedDevices.size() > 0) { //txt.setText("s3"); // Loop through paired devices for (BluetoothDevice device : pairedDevices) { // Add the name and address to an array adapter to show in a ListView mArrayAdapter.add(device.getName() + ":" + device.getAddress()); } list.setAdapter(mArrayAdapter); } } else { finish(); } } } }

效果图如下:

希望本文所述对大家的Android程序设计有所帮助。

时间: 2024-09-20 10:37:47

Android编程之蓝牙测试实例的相关文章

Android编程之蓝牙测试实例_Android

本文实例讲述了Android编程之蓝牙测试.分享给大家供大家参考.具体分析如下: 一.软件平台: win7 + eclipse + sdk 二.设计思路: 配合倒计时定时器实现蓝牙打开,可见,扫描三个功能 三.源代码: main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re

Android编程之动态壁纸实例分析_Android

本文实例讲述了Android编程之动态壁纸.分享给大家供大家参考,具体如下: 从android 2.1版本起引入了动态壁纸的概念,熟悉android的人一定不会陌生.这里解释一个动态壁纸是怎么形成又是怎么工作的. 首先动态桌面的动态体现出这个组件是实时变化的,也就是说有一个后台在不停的刷新这个组件.联想到后台组件首先想到的就是service,从代码角度看,果然如此.每一个动态桌面都继承自WallpaperService,其中必须实现的抽象方法onCreateEngine,返回一个Engine对象

Android编程之消息机制实例分析_Android

本文实例讲述了Android编程之消息机制.分享给大家供大家参考,具体如下: 一.角色描述 1.Looper: 一个线程可以产生一个Looper对象,由它来管理此线程里的Message Queue(消息队列). 2.Handler: 你可以构造Handler对象来与Looper沟通,以便push新消息到Message Queue里:或者接收Looper(从Message Queue取出)所送来的消息. 3. Message Queue(消息队列):用来存放线程放入的消息. 4.线程:UI thr

Android编程实现3D旋转效果实例_Android

本文实例讲述了Android编程实现3D旋转效果的方法.分享给大家供大家参考,具体如下: 下面的示例是在Android中实现图片3D旋转的效果. 实现3D效果一般使用OpenGL,但在Android平台下可以不直接使用OpenGL,而是使用Camera实现,Camera中原理最终还是使用OpenGL,不过使用Camera比较方便. Camera类似一个摄像机,当物体不动时,我们带着摄像机四处移动,在摄像机里面的画面就会有立体感,就可以从其它的角度观看这个物体.废话不多说,直接看示例. 运行效果如

Android编程ViewPager回弹效果实例分析_Android

本文实例讲述了Android编程ViewPager回弹效果.分享给大家供大家参考,具体如下: 其实在我们很多应用中都看到当ViewPager滑到第一页或者最后一页的时候,如果再滑动的时候,就会有一个缓冲的过程,也就是回弹效果.之前在研究回弹效果的时候,也顺便实现了ViewPager的回弹效果,其实也很简单,一下是实现代码,注释比较少: package com.freesonfish.viewpager_2; import android.content.Context; import andro

Android编程之消息机制实例分析

本文实例讲述了Android编程之消息机制.分享给大家供大家参考,具体如下: 一.角色描述 1.Looper: 一个线程可以产生一个Looper对象,由它来管理此线程里的Message Queue(消息队列). 2.Handler: 你可以构造Handler对象来与Looper沟通,以便push新消息到Message Queue里:或者接收Looper(从Message Queue取出)所送来的消息. 3. Message Queue(消息队列):用来存放线程放入的消息. 4.线程:UI thr

Android编程获取网络时间实例分析

本文实例讲述了Android编程获取网络时间的方法.分享给大家供大家参考,具体如下: 在网上看到的最常见的方式有: public static void main(String[] args) throws Exception { URL url=new URL("http://www.bjtime.cn");//取得资源对象 URLConnection uc=url.openConnection();//生成连接对象 uc.connect(); //发出连接 long ld=uc.g

Android编程之控件状态配置文件实例

本文实例讲述了Android编程之控件状态配置文件.分享给大家供大家参考,具体如下: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 无状态时 --> <item android:state_focused="false" android:state_selected="false" android:state_pr

Android编程实现3D旋转效果实例

本文实例讲述了Android编程实现3D旋转效果的方法.分享给大家供大家参考,具体如下: 下面的示例是在Android中实现图片3D旋转的效果. 实现3D效果一般使用OpenGL,但在Android平台下可以不直接使用OpenGL,而是使用Camera实现,Camera中原理最终还是使用OpenGL,不过使用Camera比较方便. Camera类似一个摄像机,当物体不动时,我们带着摄像机四处移动,在摄像机里面的画面就会有立体感,就可以从其它的角度观看这个物体.废话不多说,直接看示例. 运行效果如