今天 ,正好项目需要添加蓝牙的控制功能,我去Titianium 文档搜了一下,发现 只有Tizen 系统有,其他的都没有,只能自己做Module。
借这个机会,记录一下蓝牙控制Module 的开发过程中遇到的问题和一些知识点。
编写Module
建立项目
首先 ,建立一个Module 项目,不会的话参考:Titanium-Modules 模块开发 (一) :模块开发基础
创建完成后会是这样:
添加蓝牙相关方法
打开BluetoothadapterModule.java 文件
可看到如下代码:
23-34行 是我新添加的 蓝牙控制 的方法。
@Kroll.module(name="Bluetoothadapter", id="com.xkj.bluetooth")
public class BluetoothadapterModule extends KrollModule
{
// Standard Debugging variables
private static final String TAG = "BluetoothadapterModule";
// You can define constants with @Kroll.constant, for example:
// @Kroll.constant public static final String EXTERNAL_NAME = value;
public BluetoothadapterModule()
{
super();
}
@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
Log.d(TAG, "inside onAppCreate");
// put module init code that needs to run when the application is created
}
@Kroll.method
public void toggle() {
Log.i(TAG, "toggle Bluetooth");
// Just toggle Bluetooth power, as long as we're not already in
// an intermediate state.
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
int state = adapter.getState();
if (state == BluetoothAdapter.STATE_OFF)
adapter.enable();
else if (state == BluetoothAdapter.STATE_ON)
adapter.disable();
}
// Methods
@Kroll.method
public String example()
{
Log.d(TAG, "example called");
return "hello world";
}
// Properties
@Kroll.getProperty
public String getExampleProp()
{
Log.d(TAG, "get example property");
return "hello world";
}
@Kroll.setProperty
public void setExampleProp(String value) {
Log.d(TAG, "set example property: " + value);
}
}
当然 还需要添加权限:
打开 项目根目录下的 timodule.xml 文件
10-13行 添加蓝牙权限,
<?xml version="1.0" encoding="UTF-8"?>
<ti:module xmlns:ti="http://ti.appcelerator.org" xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Similar to tiapp.xml, but contains module/platform specific
configuration in <iphone>, <android>, and <mobileweb> sections
-->
<iphone>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
</android>
<mobileweb>
</mobileweb>
</ti:module>
蓝牙方法调用
接下来, 打开 example/app.js ,添加41行 调用刚刚新添加的方法。
剩下的都是 自动生成的一些官方给的示例代码,其中包括了 属性,方法的调用方式,初期 可以参考一下。
// This is a test harness for your module
// You should do something interesting in this harness
// to test out the module and to provide instructions
// to users on how to use it by example.
// open a single window
var win = Ti.UI.createWindow({
backgroundColor:'white'
});
var label = Ti.UI.createLabel();
win.add(label);
win.open();
// TODO: write your module tests here
var bluetoothadapter = require('com.xkj.bluetooth');
Ti.API.info("module is => " + bluetoothadapter);
label.text = bluetoothadapter.example();
Ti.API.info("module exampleProp is => " + bluetoothadapter.exampleProp);
bluetoothadapter.exampleProp = "This is a test value";
if (Ti.Platform.name == "android") {
var proxy = bluetoothadapter.createExample({
message: "Creating an example Proxy",
backgroundColor: "red",
width: 100,
height: 100,
top: 100,
left: 150
});
proxy.printMessage("Hello world!");
proxy.message = "Hi world!. It's me again.";
proxy.printMessage("Hello world!");
win.add(proxy);
}
// 调用 添加的方法 打开蓝牙
bluetoothadapter.toggle();
运行项目
然后 就可以运行一下 试试啦。
话说 怎么运行呢,看图!
首先右键 build.xml 文件,按下图选择 AntBuild ... ,打开配置窗口
配置窗口:
注意图中 红色矩形标明的地方(看不到的话,拖出去斩了~)
默认情况下 只会选中 dist 项,也就是 项目编译,
而下面的 install 会将测试项目安装到 手机上,所谓测试项目 也就是 example 中的代码部分。
注意这里的顺序, 就是第三个 红色矩形 , 需要 先编译项目,然后再安装到 手机上。
然后点击run 就可以啦,安装之后的App名称就是你Module的项目名称 ,在这里是 bluetoothadapter
安装成功的样子:
OK, 这样 打开手机之后 就可以看到啦。
如果我想知道生成的这个测试项目在哪怎么办, 来看上图中日志的倒数第二行 :
[exec] [DEBUG] /Users/ihaveu/Documents/androidsdk_/platform-tools/adb -d install -r /var/folders/50/ltj4zq6j4v94s0svnzb83mcm0000gq/T/mEUPR4uti/bluetoothadapter/build/android/bin/app.apk
明白了么? 就是这个目录:
/var/folders/50/ltj4zq6j4v94s0svnzb83mcm0000gq/T/mEUPR4uti/bluetoothadapter/
到此,打开app 之后 就可以切换蓝牙的打开状态啦。
怎么样,挺简单吧~
应用到其他项目
var bluetoothAdapter = require("com.xkj.bluetooth");
function doClick(e) {
alert($.label.text);
bluetoothAdapter.toggle();
}
$.index.open();
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
</android>
OK,接下来就万事具备,只差运行啦,试试吧。