Android藍牙聊天示例应用程序使用.doc
文本预览下载声明
本文我们将通过学习Android的蓝牙聊天示例应用程序来介绍蓝牙开发包的使用,该示例程序完整的包含了蓝牙开发的各个部分,将实现两个设备通过蓝牙进行连接并聊天。 AndroidManifest.xml 前面我们说过,在使用蓝牙API时就需要开启某些权限,同时我们还可以从AndroidManifest.xml文件中找到应用程序启动时所进入的界面Activity等信息,因此下面我们首先打开AndroidManifest.xml文件,代码如下:
复制到剪贴板??Java代码
manifest?xmlns:android=/apk/res/android? package=com.example.android.BluetoothChat? android:versionCode=1? android:versionName=1.0? !--?最小的sdk版本?--? uses-sdk?minSdkVersion=6?/? !--?权限申明?--? uses-permission?android:name=android.permission.BLUETOOTH_ADMIN?/? uses-permission?android:name=android.permission.BLUETOOTH?/? application?android:label=@string/app_name? android:icon=@drawable/app_icon?? !--?默认Activity?--? activity?android:name=.BluetoothChat? android:label=@string/app_name? android:configChanges=orientation|keyboardHidden? intent-filter? action?android:name=ent.action.MAIN?/? category?android:name=ent.category.LAUNCHER?/? /intent-filter? /activity? !--?用于显示蓝牙设备列表的Activity?--? activity?android:name=.DeviceListActivity? android:label=@string/select_device? android:theme=@android:style/Theme.Dialog? android:configChanges=orientation|keyboardHidden?/? /application? /manifest?? ??
首先minSdkVersion用于说明该应用程序所需要使用的最小SDK版本,这里设置为6,也就是说最小需要使用android1.6版本的sdk,同时Ophone则需要使用oms2.0版本,然后打开了BLUETOOTH和BLUETOOTH_ADMIN两个蓝牙操作相关的权限,最后看到了两个Activity的声明,他们分别是BluetoothChat(默认主Activity)和DeviceListActivity(显示设备列表),其中DeviceListActivity风格被定义为一个对话框风格,下面我们将分析该程序的每个细节。
BluetoothChat 首先,程序启动进入BluetoothChat,在onCreate函数中对窗口进行了设置,代码如下:
复制到剪贴板??Java代码
//?设置窗口布局? requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);? setContentView(R.layout.main);? getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,?R.layout.custom_title);?? ??
这里可以看到将窗口风格设置为自定义风格了,并且指定了自定义title布局为custom_title,其定义代码如下:
复制到剪贴板??Java代码
RelativeLayout?xmlns:android=/apk/res/android? android:layout_width=match_parent? android:layout_height=match_parent? android:gravity=center_vertical? ? TextView?android:id=@+id/title_left_text? android:layout_alignParentLeft=tr
显示全部