`
chengfengyang
  • 浏览: 21942 次
社区版块
存档分类
最新评论

android之蓝牙操作(二)

阅读更多
1、修改本蓝牙设备的可见性
2、扫描周围可用蓝牙设备

步骤:
1、在androidManifest.xml中添加以下代码
  <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>


2、在布局文件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/discoverId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="设置可见性"       
    />
    <Button 
    android:id="@+id/scanId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="开始扫描"
    />
</LinearLayout>


3、Activity文件

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 TestBluetooth02Activity extends Activity {
    /** Called when the activity is first created. */
	private Button discoverButton = null;
	private Button scanButton = null;
	private BluetoothReceiver bluetoothReceiver = null;
	private BluetoothAdapter bluetoothAdapter = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        discoverButton = (Button)findViewById(R.id.discoverId);
        discoverButton.setOnClickListener(new DiscoverButtonListener());
        //创建一个IntentFilter对象,将其action制定为BluetoothDevice.ACTION_FOUND
        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        //生成一个BluetoothReceive对象
        bluetoothReceiver = new BluetoothReceiver();
        //注册广播接收器
        registerReceiver(bluetoothReceiver,intentFilter);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        scanButton = (Button)findViewById(R.id.scanId);
        scanButton.setOnClickListener(new ScanButtonListener());
    }
    private class DiscoverButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//创建一个Intent对象,并惊奇Action的值设置为BluetoothAdapter.ACTION_REQEST_DISCOVERABLE
			Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			//将一个键值对放在Intent对象中,主要用于指定可见状态的持续时间
			discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
			startActivity(discoverIntent);
		}
    	
    	
    }
    private class BluetoothReceiver extends BroadcastReceiver
    {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				//可以从收到的Intent对象当中,将代表远程的蓝牙适配器的对象取出
				BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				System.out.println(device.getAddress());
			}
		}
    	
    	
    }
    private class ScanButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			bluetoothAdapter.startDiscovery();
		}
    	
    }
  /*  protected void onDestroy
    {
    	unregisterReceiver(bluetoothReceiver);
    	super.onDestroy();
    }*/
}
0
0
分享到:
评论

相关推荐

    android studio 蓝牙开发demo

    总的来说,"android studio 蓝牙开发demo"涵盖了Android Bluetooth LE开发的基本流程,从设备搜索、连接、服务发现到数据交换,是学习和实践Android蓝牙编程的好素材。通过学习这个示例,开发者可以构建自己的蓝牙...

    Android 打开蓝牙耳机录音

    在Android平台上,实现“蓝牙耳机录音”涉及到一系列的系统服务、API接口以及音频处理技术。以下将详细解析这个过程,并提供关键知识点。 首先,我们来理解“Sco”(Short Range Communication)模式。Sco是蓝牙...

    ANDROID9.0 蓝牙主从切换方案.rar

    此外,对蓝牙操作进行优化,减少不必要的资源消耗,也是提升用户体验的重要环节。 通过以上知识点的介绍,我们可以理解到在Android 9.0中实现蓝牙主从模式一键切换涉及到的技术和挑战。这个方案为开发者提供了更大...

    android studio 蓝牙 socket 范例

    综上所述,这个范例涵盖了Android蓝牙通信的基础,对于想要学习如何在Android应用中使用蓝牙功能的开发者来说非常有价值。通过深入理解这些知识点,你可以创建自己的蓝牙应用,实现设备之间的数据交换。

    android连接蓝牙实例

    在整个蓝牙操作过程中,可能会遇到各种异常,如设备未开启、连接失败、数据传输错误等。因此,良好的异常处理机制是必不可少的,确保在出现问题时能够及时反馈并尝试恢复。 9. **权限管理**: Android应用使用...

    android监听蓝牙设备的连接状态

    在Android平台上,监听蓝牙设备的连接状态是一项关键功能,它涉及到设备间的无线通信与数据交换。本文将深入探讨如何利用Android系统广播机制来实现这一目标。 首先,我们要明白蓝牙连接状态主要包括三种:未连接、...

    Android蓝牙广播通信

    Android提供了一个名为BluetoothAdapter的类,它是Android蓝牙功能的主要入口点。通过这个类,我们可以检查蓝牙是否开启,扫描附近的蓝牙设备,并建立连接。当我们要监听蓝牙设备的状态变化时,就需要用到广播通信。...

    Android之蓝牙驱动开发总结

    BlueZ库是用户空间中的库,提供蓝牙操作的库文件libbluetooth.so。适配库包含特定硬件平台的适配层。而Java框架层则主要实现了蓝牙相关的服务和配置文件,比如Headset/Handsfree和A2DP/AVRCP等。在应用层,开发者...

    Android Studio 最新蓝牙服务端源码 蓝牙通信代码BluetoothService.rar

    在Android平台上,开发蓝牙通信应用是一项常见的任务,尤其在物联网(IoT)设备连接和数据交换的场景下。本文将详细解析"Android Studio 最新蓝牙服务端源码 蓝牙通信代码BluetoothService.rar"中涉及的关键知识点,...

    Android利用蓝牙广播数据

    在Android平台上,蓝牙技术是一种广泛使用的短距离无线通信方式,特别是在设备间的数据交换和通信方面。本文将深入探讨如何在Android设备上利用蓝牙广播数据,以及这种技术在Android与可穿戴设备之间的通信应用。 ...

    Android蓝牙连接设备demo

    "Android蓝牙连接设备demo"是一个示例项目,它演示了如何使用Android SDK来实现BLE 4.0及更高版本的蓝牙设备连接和通信功能。下面我们将深入探讨这个demo涉及的关键知识点。 1. **BLE 4.0与Android支持**:BLE 4.0...

    android BlueToothDemo (蓝牙搜索,配对,连接,传输)详细讲解见博客

    在`BlueToothDemo`项目中,`BooleanTeethDemo`很可能是一个演示程序,用于展示蓝牙操作的基本流程。让我们一步步解析这个过程: 1. **蓝牙搜索**: 蓝牙搜索通常通过BluetoothAdapter的`startDiscovery()`方法进行...

    Kotlin+Android 蓝牙基本操作

    采用kotlin 对蓝牙进行打开和关闭,以及搜索蓝牙设备,与其他蓝牙设备配对,并将所有配对的信息进行显示。相关说明看 https://mp.csdn.net/mp_blog/creation/editor/119547298

    Android Studio蓝牙通信客户端Demo源码BTClient.rar

    在Android平台上进行应用程序开发时,Android Studio是Google官方推荐的...通过深入研究和理解这个BTClient Demo,开发者可以掌握Android蓝牙通信的基本原理和实践技巧,为自己的应用添加蓝牙通信功能打下坚实基础。

    android通用蓝牙调试助手

    总结,"Android通用蓝牙调试助手"作为一款强大的BLE调试工具,通过其便捷的操作和全面的功能,为Android开发者提供了有力的支持。掌握并熟练运用这款工具,无疑会提升开发者的蓝牙应用能力,加速项目的进展。

    android 蓝牙数据传输DEMO

    这些类位于`android.bluetooth`包中,是进行蓝牙操作的基础。 2. **蓝牙适配器(BluetoothAdapter)**:它是Android设备的蓝牙接口,用于检查蓝牙是否开启、获取已配对设备以及开启/关闭蓝牙服务等操作。 3. **...

    android通过蓝牙进行通讯

    在Android平台上,蓝牙通信是一种常见的无线数据传输方式,尤其适用于设备间的短距离通信。这篇博客主要探讨了如何在...通过这些步骤,开发者可以构建一个基本的Android蓝牙通信应用,实现设备间的无线数据交换。

    android蓝牙聊天源代码

    蓝牙操作可能会遇到各种错误,如设备未开启、连接失败等,需要适当地捕获并处理异常,提供友好的用户体验。 11. **连接管理和断开**: 应用程序需要管理蓝牙连接的状态,确保在不再需要时关闭连接,节约资源。`...

    android 蓝牙SPP传输demo

    - 在蓝牙操作中,常见的异常包括`IOException`、` BluetoothStateException`等,需要捕获并处理。 - 断开连接时,确保关闭`InputStream`、`OutputStream`和`BluetoothSocket`以释放资源。 6. **权限声明**: - ...

    Android蓝牙开发客户端代码

    首先,我们需要理解蓝牙技术的基本概念,然后我们将详细讨论Android蓝牙开发客户端的实现步骤。 蓝牙是一种短距离无线通信技术,允许电子设备之间进行数据交换。在Android中,蓝牙功能主要通过`BluetoothAdapter`、...

Global site tag (gtag.js) - Google Analytics