- 浏览: 84185 次
- 性别:
- 来自: 广州
-
最新评论
-
Tony_Lee-S:
已经说得很明白了吧?“把list强转成Serializable ...
在android中用 Intent 传递 List类型的数据(转) -
zhiduo5:
Activity与Service之间传递参数的介质这个怎么弄啊 ...
在android中用 Intent 传递 List类型的数据(转) -
mathlove:
很有用的内容
一个Android下的自动下载歌词的代码(转)
一. 修改本机蓝牙设备的可见性
二. 扫描周围可用的蓝牙设备
Eg:
一. 清单文件AdroidManifest.xml:
二. 布局文件: main.xml:
三. MainActivity:
转自:
http://blog.csdn.net/t12x3456/article/details/7865690
二. 扫描周围可用的蓝牙设备
Eg:
一. 清单文件AdroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.se7en" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.BLUETOOTH"/> <!-若需要管理蓝牙设备,如修改可见性,则需以下的权限-> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> </manifest>
二. 布局文件: 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()); } } }
转自:
http://blog.csdn.net/t12x3456/article/details/7865690
发表评论
-
SQLite多线程读写实践及常见问题总结(转)
2013-02-07 11:04 3073基本操作的部分,大家都很熟悉了,这里根据个人切身经验,总结了 ... -
android内存溢出的解决方法(转)
2013-02-07 11:23 8371. 当项目中包含大量图片,或者图片过大 方法1:等比例缩 ... -
android中的跨进程通信的实现——远程调用过程和aidl(转)
2013-02-07 19:35 1369android在设计理念上强调组件化,组件之间的依赖性很小。 ... -
在EditText/TextView中插入表情图片、样式、下划线等(转)
2012-08-22 12:01 1628EditText: 通常用于显示文字,但有时候也需要在文字中 ... -
自定义Tab选项卡(转)
2012-08-21 08:44 1534效果: 代码: import android.app. ... -
android设置button透明度(转)
2012-08-18 22:09 1386Button或者ImageButton的背景设为透明或者半透明 ... -
Android中的TabHost(转)
2012-08-17 14:50 2245介绍 有时,我们想在一个window中显示多个视图,这时就需 ... -
Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备(转)
2012-08-15 16:49 1765一. 什么是蓝牙(Bluetooth)? 1.1 BuleT ... -
一个Android下的自动下载歌词的代码(转)
2012-08-15 13:41 12391. 原理是用Baidu音乐搜索的连接,拼参数进去读取歌词。 ... -
ViewPager多页面滑动切换以及动画效果(转)
2012-08-14 16:55 1919一、首先,我们来看一下效果图,这是新浪微博的Tab滑动效果。我 ... -
Android的Handler总结(转)
2012-08-14 14:18 958一、Handler的定义: 主要接受子线程 ... -
AsyncTask的用法(转)
2012-08-14 14:11 919在开发Android应用时必须遵守单线程模型的原则: Andr ... -
Android逐帧动画(转)
2012-08-13 16:46 1809Android有两种动画,一种是tweened animati ... -
android 扫描SD卡与系统文件(转)
2012-08-11 16:12 1379当手机或模拟器开机时,会调用android的MediaScan ... -
android 怎样为多媒体文件生成缩略图(转)
2012-08-10 17:11 13081、Video 对于视频,取第一帧作为缩略图,也就是怎样从fi ... -
TextView 实现自动换行(转)
2012-08-10 16:44 1432package com.liao.intentservice; ... -
android:屏幕自适应(转)
2012-08-10 16:10 1422demo下载: http://www.eoeandroid.c ... -
listview加载性能优化ViewHolder (转)
2012-07-28 15:16 1489在android开发中Listview是一个很重要的组件,它以 ... -
android 应用程序Activity之间数据传递与共享的几种途径(转)
2012-07-19 11:03 15391.基于消息的通信机制 Intent ---boudle , ... -
Android进程间通信--消息机制及IPC机制实现(转)
2012-07-19 10:51 866一、概念及说明 Android ...
相关推荐
在本示例"蓝牙Demo(设置蓝牙可见性 扫描周围蓝牙)"中,我们将探讨如何通过编程实现蓝牙设备的可见性设置以及扫描周围可用的蓝牙设备。 首先,蓝牙设备的可见性是指该设备是否允许其他蓝牙设备发现它。在Android...
3. **蓝牙可见性**:为了让其他设备能发现你的设备,你需要设置蓝牙为可见模式。这可以通过`BluetoothAdapter`的`makeDiscoverable()`方法实现,该方法会启动一个广播,使设备在一段时间内可被其他设备找到。 4. **...
此外,关注蓝牙适配器的状态变化,如是否正在扫描或是否已连接到其他设备,以确保操作的有效性。 9. **错误处理和优化** 蓝牙连接可能会遇到各种问题,如设备不可见、连接超时、配对失败等。因此,良好的错误处理...
`WifiManager`是Android系统中管理WiFi连接的核心API,提供了控制WiFi状态、扫描可用网络、获取当前连接信息等功能。开发者可以通过`Context.getSystemService(Context.WIFI_SERVICE)`方法获取`WifiManager`实例,...
开发者需要检查设备是否支持蓝牙,开启或关闭蓝牙,并获取可用的蓝牙设备。`BluetoothSocket`用于建立连接,`InputStream`和`OutputStream`则用于数据传输。游戏状态和玩家的每一步操作都会通过蓝牙发送到对手的设备...
用户可以在这里开启/关闭Wi-Fi,扫描可用的Wi-Fi网络,以及连接到特定的网络。`WifiEnabler`类是用来处理Wi-Fi开关状态的,它监听Wi-Fi状态的变化,并更新UI。 6. **显示设置(Display)** 显示设置包括亮度、屏幕...