android不断开关蓝牙
来源:互联网 发布:淘宝无法开店 编辑:程序博客网 时间:2024/05/22 05:57
文章的思路就是在主线程中设置一个按钮,启动一个线程,在子线程中进行线程的耗时操作,最后需要记住添加蓝牙权限
AndroidManifest.xml中加入
package com.example.testcamera;import java.io.IOException;import android.R.anim;import android.support.v7.app.ActionBarActivity;import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.content.Intent;import android.content.res.Configuration;import android.hardware.Camera;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {Button startButton;TextView textView;Handler handle = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); textView.setText(" "+msg.arg1); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton=(Button) findViewById(R.id.startButton); textView=(TextView) findViewById(R.id.tv); startButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {MyThread myThread=new MyThread();myThread.start();}}); } class MyThread extends Thread { @Override public void run() { // 耗时操作 int num=0; while(true){ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); mBluetoothAdapter.disable();//关闭蓝牙 String string1=mBluetoothAdapter.getAddress(); Log.e("test", "关闭"+mBluetoothAdapter.getState()); try {Thread.sleep(15000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} mBluetoothAdapter.enable(); String string=mBluetoothAdapter.getAddress(); Message msg = new Message(); num++; msg.arg1=num; Log.e("bluetest", "开启"+mBluetoothAdapter.getState()); Log.e("bluetest", string); Log.e("bluetest","总的次数"+num); handle.sendMessage(msg); try {Thread.sleep(15000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} super.run(); } } }}
这里我在主界面更新开关的次数,也将开关的状态的返回值,打开的返回值,蓝牙的mac地址,和开关的次数输出到了log之中。
这里,谢谢我同事b.z对我的提醒
0 0
- android不断开关蓝牙
- Android 蓝牙开关
- android 蓝牙 开关的方法
- Android快捷开关实现 Wifi开关、蓝牙开关、GPS开关、同步开关、亮度设置开关
- Android 命令行控制蓝牙开关【转】
- Android TV开发之快捷开关打开WiFi和蓝牙
- 命令行控制蓝牙开关
- 票据打印机-蓝牙的开关
- android 常用功能的切换(wifi开关,蓝牙开关,飞行模式切换,响铃模式切换,亮度切换)
- 虾扯蓝牙(一)获取蓝牙当前状态,监听蓝牙手动开关,代码开关蓝牙
- android物联网初步,利用手机蓝牙与单片机通信,实现led灯开关和定时
- android-----开关ActionBar,开关状态栏
- 获取蓝牙开关状态以及开关蓝牙----苏苏的小弟子
- android 蓝牙
- Android 蓝牙
- android 蓝牙
- Android蓝牙
- Android 蓝牙
- AbstractQueuedSynchronizer的介绍和原理分析
- 小事
- 关于反爬虫,看这一篇就够了
- 【淘淘】Spring整合Quartz框架
- 斐波那契数列 矩阵求法 优化
- android不断开关蓝牙
- rgba中的a是指?CSS之RGBA颜色指南
- 警告:不能读取 AppletViewer 的属性文件解决
- SQLserver学习day08 视图和索引
- NYoj 906杨辉三角(二维数组)
- 勾选问题
- [2016/12/6]计算两个大数的和
- C# 如何关联键盘按钮 (KeyChar/KeyCode值 KeyPress/KeyDown事件 区别)
- docker搭建 存储 运行 容器