求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 
 

Android开发教程
Android 开发环境配置
Android 架构
Android 应用组件
Android Hello World示例
Android 资源组织和访问
Android Activity
Android Service
Android 广播接收器
Android 内容提供者
Android 碎片/片段
Android Intent过滤器
Android UI布局
Android UI控件
Android 事件处理
Android 样式和主题
Android 自定义组件
Android 拖放
Android 通知
Android 基于位置服务
Android 发送电子邮件
Android 发送短信/SMS
Android 拨打电话
发布Android应用
ndroid Alertdialog
Android Animation实例
Android音频捕获
Android音频管理器实例
Android
Android最佳实践
Android Bluetooth实例
Android Camera
Android Clipboard
Android自定义字体
Android数据备份
Android Gestures/手势
Android图片效果
Android图片切换
Android内部存储
Android JetPlayer实例
Android JSON解析器
Android加载Spinner
Android本地化
Android登录实例
Android MediaPlayer
 
 

Android Bluetooth(蓝牙)实例
795 次浏览
35次  

在很多方面,蓝牙是一种能够发送或接受两个不同的设备之间传输的数据。 Android平台包含了蓝牙框架,使设备以无线方式与其他蓝牙设备进行数据交换的支持。

Android提供蓝牙API来执行这些不同的操作。

  1. 扫描其他蓝牙设备
  2. 获取配对设备列表
  3. 连接到通过服务发现其他设备

Android提供BluetoothAdapter类蓝牙通信。通过调用创建的对象的静态方法getDefaultAdapter()。其语法如下给出。

private BluetoothAdapter BA;
    BA = BluetoothAdapter.getDefaultAdapter();

为了使用设备的蓝牙,调用下列蓝牙ACTION_REQUEST_ENABLE的意图。其语法如下:

Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(turnOn, 0);       

除了这个常量,有提供其它的API,支持不同任务的其他常数。它们在下面列出。

启用了蓝牙功能之后,可以通过调用 getBondedDevices()方法来获取配对设备列表。它返回一组的蓝牙设备。其语法如下:

private Set<BluetoothDevice>pairedDevices;
    pairedDevices = BA.getBondedDevices();

除了配对的设备,还有API,让更多蓝牙控制权等方法。它们在下面列出。

例子

这个例子提供了示范BluetoothAdapter类操纵蓝牙,并显示通过蓝牙配对设备列表。

为了试验这个例子,需要在实际设备上运行此程序

以下是?src/com.yiibai.bluetooth/MainActivity.java?文件的内容:

package com.example.bluetooth;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    import android.os.Bundle;
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    public class MainActivity extends Activity {
       private Button On,Off,Visible,list;
       private BluetoothAdapter BA;
       private Set<BluetoothDevice>pairedDevices;
       private ListView lv;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        On = (Button)findViewById(R.id.button1);
        Off = (Button)findViewById(R.id.button2);
        Visible = (Button)findViewById(R.id.button3);
        list = (Button)findViewById(R.id.button4);
          lv = (ListView)findViewById(R.id.listView1);
          BA = BluetoothAdapter.getDefaultAdapter();
     }
       public void on(View view){
        if (!BA.isEnabled()) {
           Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
           startActivityForResult(turnOn, 0);
           Toast.makeText(getApplicationContext(),"Turned on"
            ,Toast.LENGTH_LONG).show();
        }
        else{
           Toast.makeText(getApplicationContext(),"Already on",
           Toast.LENGTH_LONG).show();
           }
     }
     public void list(View view){
        pairedDevices = BA.getBondedDevices();
          ArrayList list = new ArrayList();
          for(BluetoothDevice bt : pairedDevices)
          list.add(bt.getName());
          Toast.makeText(getApplicationContext(),"Showing Paired Devices",
          Toast.LENGTH_SHORT).show();
          final ArrayAdapter adapter = new ArrayAdapter
          (this,android.R.layout.simple_list_item_1, list);
        lv.setAdapter(adapter);
       }
     public void off(View view){
        BA.disable();
        Toast.makeText(getApplicationContext(),"Turned off" ,
        Toast.LENGTH_LONG).show();
     }
     public void visible(View view){
        Intent getVisible = new Intent(BluetoothAdapter.
        ACTION_REQUEST_DISCOVERABLE);
        startActivityForResult(getVisible, 0);
       }
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
     }
    }

这里是?activity_main.xml?文件的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context=".MainActivity" >
       <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
       <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
       <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textAppearance="?android:attr/textAppearanceLarge" />
       <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="on"
        android:text="@string/on" />
       <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="visible"
        android:text="@string/Visible" />
       <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="list"
        android:text="@string/List" />
       <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:onClick="off"
        android:text="@string/off" />
       <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible" >
       </ListView>
       </LinearLayout>
  </ScrollView>
</RelativeLayout>

这里是?Strings.xml?文件的内容:

<?xml version="1.0" encoding="utf-8"?>
   <resources>
       <string name="app_name">Bluetooth</string>
       <string name="action_settings">Settings</string>
       <string name="hello_world">Hello world!</string>
       <string name="on">Turn On</string>
       <string name="off">Turn Off</string>
       <string name="Visible">Get Visible</string>
       <string name="List">List Devices</string>
    </resources>

这里是?AndroidManifest.xml?文件的内容:

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.yiibai.bluetooth"
     android:versionCode="1"
     android:versionName="1.0" >
       <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
     <uses-permission android:name="android.permission.BLUETOOTH"/>
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
       <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
           android:name="com.yiibai.bluetooth.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>
    </manifest>

让我们试着运行AndroidCapture应用程序。假设你已经连接实际的Android移动设备到计算机。启动应用程序之前,Eclipse会显示如下窗口,选择要运行的Android应用程序的选项。

选择移动设备作为一个选项,然后检查移动设备将显示如下界面:

现在选择打开开启蓝牙。但是当选择它,蓝牙将不会被打开。事实上它会询问许可,以启用蓝牙。

现在,只需要选择设置可见按钮来打开视图。下面的屏幕会出现要求许可才能打开发现120秒。

现在,只要选择列表中的设备选项。它会列出倒在列表视图中的配对设备。就我而言,只有一个配对设备。它如下所示。

现在,只需选择关闭按钮来关闭蓝牙。当关掉蓝牙指示成功切换关闭蓝牙会出现以下消息。



您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



795 次浏览
35次
 捐助