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

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是蓝牙...

    android连接蓝牙实例

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

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

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

    android4.0蓝牙收发数据demo

    在Android 4.0(Ice Cream Sandwich)系统中,蓝牙功能得到了显著的增强,特别是引入了Bluetooth Low Energy(BLE)技术,使得设备间的数据通信更加高效且节能。本教程将详细解析如何创建一个简单的Android应用,实现...

    Android利用蓝牙广播数据

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

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

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

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

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

    Android蓝牙连接设备demo

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

    android通用蓝牙调试助手

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

    Kotlin+Android 蓝牙基本操作

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

    android通过蓝牙进行通讯

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

    android蓝牙聊天源代码

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

    android 蓝牙SPP传输demo

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

    Android蓝牙开发客户端代码

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

    AndroidStudio蓝牙接收数据绘图

    - 蓝牙操作可能抛出各种异常,如`IOException`、`NullPointerException`等,需要妥善处理,提供良好的用户体验。 9. **性能优化**: - 使用`Handler`或`HandlerThread`进行异步操作,避免阻塞主线程。 - 数据...

    基于android的蓝牙功能实现

    通过这个项目,开发者可以学习如何在实际应用中整合这些蓝牙操作。 总的来说,实现基于Android的蓝牙功能需要理解蓝牙适配器、设备、服务器套接字、客户端套接字的概念,以及如何进行数据传输。提供的源码...

    Android ble 蓝牙库

    本Android BLE蓝牙库是为了简化开发者对BLE设备的操作而设计的,它提供了一个方便、高效的接口,使开发者能够快速集成到自己的应用程序中。 一、BLE简介 Bluetooth Low Energy,通常称为BLE或Bluetooth Smart,是...

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

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

    Android蓝牙操作源代码

    这份"Android蓝牙操作源代码"提供了一套完整的解决方案,涵盖了从扫描蓝牙设备到建立连接以及进行数据通信的关键步骤。以下是对这些关键知识点的详细阐述: 1. **蓝牙开启与扫描** 在Android中,使用`...

Global site tag (gtag.js) - Google Analytics