一. 修改本机蓝牙设备的可见性

二. 扫描周围可用的蓝牙设备

一.  清单文件AdroidManifest.xml:

<uses-permission android:name="android.permission.BLUETOOTH"/>

<!-若需要管理蓝牙设备,如修改可见性,则需以下的权限->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

二. 布局文件: main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/discoverButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="设置可见性"/>
<Button
android:id="@+id/scanButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始扫描"/>
</LinearLayout>

  

三. MainActivity:

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.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity {
private Button discoverButton = null;
private Button scanButton = null;
private BluetoothAdapter adapter = null;
private BluetoothReceiver bluetoothReceiver = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); adapter = BluetoothAdapter.getDefaultAdapter(); discoverButton = (Button)findViewById(R.id.discoverButton);
scanButton = (Button)findViewById(R.id.scanButton);
//修改蓝牙设备的可见性
discoverButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); //设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
startActivity(discoverIntent);
}
}); scanButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个BroadcastReceiver来获取信息
adapter.startDiscovery();
}
}); //设定广播接收的filter
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//创建蓝牙广播信息的receiver
bluetoothReceiver = new BluetoothReceiver ();
//注册广播接收器
registerReceiver(bluetoothReceiver,intentFilter); } private class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//获得扫描到的远程蓝牙设备
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
} }
}

Android开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备的更多相关文章

  1. Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备

    版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 修改本机蓝牙设备的可见性 二. 扫描周围可用的蓝牙设备 Eg: 一.  清单文件AdroidManifest.xml: <?xml v ...

  2. 【视频】零基础学Android开发:蓝牙聊天室APP(四)

    零基础学Android开发:蓝牙聊天室APP第四讲 4.1 ListView控件的使用 4.2 BaseAdapter具体解释 4.3 ListView分布与滚动事件 4.4 ListView事件监听 ...

  3. 【视频】零基础学Android开发:蓝牙聊天室APP(二)

    零基础学Android开发:蓝牙聊天室APP第二讲 2.1 课程内容应用场景 2.2 Android UI设计 2.3 组件布局:LinearLayout和RelativeLayout 2.4 Tex ...

  4. 【视频】零基础学Android开发:蓝牙聊天室APP(三)

    零基础学Android开发:蓝牙聊天室APP第三讲 3.1 ImageView.ImageButton控件具体解释 3.2 GridView控件具体解释 3.3 SimpleAdapter适配器具体解 ...

  5. 【视频】零基础学Android开发:蓝牙聊天室APP(一)

    零基础学Android开发:蓝牙聊天室APP第一讲 1. Android介绍与环境搭建:史上最高效Android入门学习 1.1 Google的大小战略 1.2 物联网与云计算 1.3 智能XX设备 ...

  6. Android开发之蓝牙--扫描已经配对的蓝牙设备

    一. 什么是蓝牙(Bluetooth)? 1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) 1.3  常用于连接耳机,鼠标和移动通讯设备等. 二. ...

  7. Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备

    版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 什么是蓝牙(Bluetooth)? 1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) ...

  8. macbook Android开发环境搭建,真机调试

    买了一台MacBook,本以为可以鼓捣一下iOS开发之类的,可惜导师要我做Android开发.无奈开始了在MacBook上开发Android的工作. 从开始配置环境到应用成功在真机上运行,也是曲曲折折 ...

  9. android开发关于和使用本机内存、内置存储卡和外置存储卡 (转)

    转自:http://www.2cto.com/kf/201304/204729.html 关于android存储器简介:                  android开发常常需要涉及数据缓存,这就 ...

随机推荐

  1. firemonkey中stringgrid属性大全

    StringGrid之属性大全: Align:                            //确定组件在父类组件区内的对齐方式(alScale:随窗口 放大缩小) AlterRowBack ...

  2. C# 基本算法

    1.冒泡排序 排序 ,,,,,, }; ; i < ArrayList.Count(); i++) { for (int j = i; j < ArrayList.Count(); j++ ...

  3. <改变未来的九大算法>读书笔记二

    原理 数据库的一致性 1.事务和代办事项表把戏(预写日志记录) 1.代办事项表把戏:先把要执行的的操作写入硬件,即写日志.即使数据库操作错误,也可根据日志来纠正.对日志的操作具有等幂性,即日志中的每项 ...

  4. android中 EditTex t的 inputType 属性

    //文本类型,多为大写.小写和数字符号    android:inputType="none"    android:inputType="text"    a ...

  5. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

  6. fgets和fputs函数

    1 函数输入 下面两个函数提供每次输入一行的功能. #include <stdio.h> char *fgets( char *restrict buf, int n, FILE *res ...

  7. 利用NVelocity 模版生成文本文件

    namespace Common { public class Tools { public string Process(string content, int startIndex, int le ...

  8. 居于集成了adt的Android 开发环境配置

    一.先说一下环境 Windows 8.1 64 位 注:win7 Ultimate  64 配置会出现 Android SDK manger 不能启动的问题,是因为android.bat 里调用了fi ...

  9. ubuntu 安装pyqt4 eric

    tar xvf eric4-4.5.7.tar.gztar xvf eric4-i18n-zh_CN.GB2312-4.5.7.tar.gzcd eric4-4.5.7/python install. ...

  10. Zabbix_server.conf 的性能调优

    Zabbix安装完成后,模板里面有一个Template App Zabbix Server,添加到zabbix服务器里. 过个一两天,查看以下的图表(在Graphs里面). Zabbix cache ...