标签:mtk gpio bluetooth-s sharedpreferences
mtk硬件启动关闭蓝牙功能的项目:mtk上层操作GPIO应用示例
项目要求:
接上篇:
1:蓝牙按键(KCOL2+KROW1)长按3秒,软件上控制GPIO144拉高2秒后关闭,蓝牙就开启并搜索配对。同时拉高GPIO98,控制音频的模拟开关切换蓝牙音源。
2:蓝牙在工作状态下,给出个1.8V的高电平,给GPIO145用来检测蓝牙的工作状态。
3:蓝牙在工作装态下想要重新配对,在长按蓝牙按键3秒,控制GPIO97拉高3秒后关闭,蓝牙模块在收到这个信号后执行清空列表和重新搜索功能。
4:蓝牙按键(KCOL2+KROW1)长按8秒,软件上控制GPIO144拉高2秒后关闭,蓝牙模块就关闭了。
本篇讲述如何控制。
1, 设置蓝牙控制GPIO为初始状态,GPIO97 98 144全部设置为GPIOOUT,且下拉。GPIO145设置为GPIOIN。
2,侦测开机事件boot_completed,在这个intent处理程序中,检查bt的switched的状态,如果为//checked状态,那么打开蓝牙,如果为非checked状态,则不理会。
第一步,在AndroidManifest.xml中添加:
<receiverandroid:name="BootCompletedReceiver">
<intent-filter>
<actionandroid:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
在当前包下面建立BootCompletedReceiver.java文件
我们定义两个SharedPreferences来保存系统的bt和ethernet状态,因为省电需求,我们每次开机都保持Ethernet和BT为关闭。
packagecom.mediatek.oobe;
importandroid.app.Activity;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.SharedPreferences;
importandroid.util.Log;
publicclassBootCompletedReceiver extendsBroadcastReceiver {
@Override
publicvoidonReceive(Context mContext, Intent arg1) {
Log.e("zcfdebug","wewill set BT and ethernet default to off!");
SharedPreferencesbtSwitchSharedPreferences=mContext.getSharedPreferences("OOBE_Settings",Activity.MODE_PRIVATE);
SharedPreferences.EditormEditor=btSwitchSharedPreferences.edit();
mEditor.putString("btSwitch","0");
mEditor.putString("eth0Switch","0");
}
}
//在这个类里面,我们要检测蓝牙的当前设置是否为开或者关。
3,我们打开图形界面,显示以太网卡开关和蓝牙开关的时候,需要根据这里的SharedPreferences来决定开关显示状态,这个需要在开关的Enable类的初始化完成:
在蓝牙的BtEnabler类里面增加三个私有成员
privateSharedPreferences btSwitchSharedPreferences;
privateString btSwitchStatusString;
privateSharedPreferences.Editor mEditor;
在Enable类的初始化函数中间根据SharedPreferences的状态设置UISwitch开关状态。
publicBtEnabler(Context context, Preference switchPref) {
mContext= context;
mBtSwitchPre= (SwitchPreference) switchPref;
//mBtSwitchPre.setChecked(false);
booleanret= EmGpio.gpioInit();
btSwitchSharedPreferences=context.getSharedPreferences("OOBE_Settings",Activity.MODE_PRIVATE);
btSwitchStatusString=btSwitchSharedPreferences.getString("btSwitch",null);
mEditor=btSwitchSharedPreferences.edit();
if(btSwitchStatusString=="0"){
mBtSwitchPre.setChecked(false);
}elseif(btSwitchStatusString=="1"){
mBtSwitchPre.setChecked(true);
}else{
mBtSwitchPre.setChecked(false);
}
}
在用户打开关闭蓝牙的处理函数中间,进行SharedPreferences的设置
OnPreferenceChangeListenermBtPreferenceChangeListener= newOnPreferenceChangeListener() {
@Override
publicbooleanonPreferenceChange(Preference preference, Object newValue) {
//TODOAuto-generated method stub
booleanisChecked = (Boolean) newValue;
if(isChecked){
mEditor.putString("btSwitch","1");
}else{
mEditor.putString("btSwitch","0");
}
setBtEnable(isChecked);
}
returntrue;
}
};
对于以太网,因为其他程序也会进行设置,所以,这里也是类似操作。
(注意,为了系统省电的操作,我们没有使用系统所提供的EthernetManager的开关,而是直接关闭电源)
4,接收系统发出的android.intent.action.onkey.BT_START
第一步,在AndroidManifest.xml中添加:
<receiverandroid:name="BtStartReciver">
<intent-filter>
<actionandroid:name="android.intent.action.onkey.BT_START"/>
</intent-filter>
</receiver>
在当前包里面建立BtStartReciver.java,内容如下
packagecom.mediatek.oobe;
importandroid.app.PendingIntent;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.util.Log;
public classBtStartReciver extends BroadcastReceiver {
@Override
public voidonReceive(Context mContext, Intent arg1) {
Log.e("zcfdebug","wewill set BT according preference!");
}
}
在这里文件里,我们将按照项目要求,如果当前蓝牙处于关的状态,则打开蓝牙。否则,不理会。
蓝牙开关状态保存在设置界面中,而且第一次开机必须为关闭状态。
首先我们读取蓝牙的SharedPreferences,判断当前的BT是否为关闭状态,如果是,那么打开蓝牙,并且更新SharedPreferences。代码如下:
packagecom.mediatek.oobe;
importcom.mediatek.engineermode.io.EmGpio;
importandroid.app.Activity;
importandroid.app.PendingIntent;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.SharedPreferences;
importandroid.util.Log;
publicclass BtStartReciver extends BroadcastReceiver {
privateSharedPreferences btSwitchSharedPreferences;
privateString btSwitchStatusString;
privateSharedPreferences.Editor mEditor;
@Override
publicvoid onReceive(Context mContext, Intent arg1) {
Log.e("zcfdebug","wewill set BT according preference!");
btSwitchSharedPreferences=mContext.getSharedPreferences("OOBE_Settings",Activity.MODE_PRIVATE);
btSwitchStatusString=btSwitchSharedPreferences.getString("btSwitch",null);
mEditor=btSwitchSharedPreferences.edit();
if(btSwitchStatusString=="0"){
// mBtSwitchPre.setChecked(false);
mEditor.putString("btSwitch","1");
setBtEnable(true);
}
}
privatevoid setBtEnable(boolean isChecked){
EmGpio.setGpioOutput(144);
EmGpio.setGpioOutput(98);
if(isChecked){
newThread(new Runnable() {
publicvoid run() {
Log.e("zcfdebug","weset GPIO 144 and 98 high");
EmGpio.setGpioDataHigh(144);
EmGpio.setGpioDataHigh(98);
Log.e("zcfdebug","wewill pull 144 2senconds");
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("zcfdebug","wewill pull 144 down");
EmGpio.setGpioDataLow(144);
}
}).start();
}else{
newThread(new Runnable() {
publicvoid run() {
Log.e("zcfdebug","weset GPIO 144 high and 98 low");
EmGpio.setGpioDataHigh(144);
EmGpio.setGpioDataLow(98);
Log.e("zcfdebug","wewill pull 144 2senconds");
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("zcfdebug","wewill pull 144 down");
EmGpio.setGpioDataLow(144);
}
}).start();
}
}
}
4,接收系统发出的android.intent.action.onkey.BT_STOP
与上面的Start方法一样的,就不累述。
5,上面的GPIO操作才有的mtk提供的方式,jni方法集成在系统so文件中libem_gpio_jni.so
其接口代码如下EmGpio.java:
/*Copyright Statement:
*
* Thissoftware/firmware and related documentation ("MediaTekSoftware") are
*protected under relevant copyright laws. The information containedherein
* isconfidential and proprietary to MediaTek Inc. and/or its licensors.
*Without the prior written permission of MediaTek inc. and/or itslicensors,
* anyreproduction, modification, use or disclosure of MediaTek Software,
* andinformation contained herein, in whole or in part, shall be strictlyprohibited.
*
*MediaTek Inc. (C) 2010. All rights reserved.
*
* BYOPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES ANDAGREES
* THATTHE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEKSOFTWARE")
*RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TORECEIVER ON
* AN"AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY ANDALL WARRANTIES,
*EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIEDWARRANTIES OF
*MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TOTHE
*SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLYTO SUCH
* THIRDPARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLYACKNOWLEDGES
* THATIT IS RECEIVER‘S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTYALL PROPER LICENSES
*CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BERESPONSIBLE FOR ANY MEDIATEK
*SOFTWARE RELEASES MADE TO RECEIVER‘S SPECIFICATION OR TO CONFORM TO APARTICULAR
*STANDARD OR OPEN FORUM. RECEIVER‘S SOLE AND EXCLUSIVE REMEDY ANDMEDIATEK‘S ENTIRE AND
*CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASEDHEREUNDER WILL BE,
* ATMEDIATEK‘S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE ATISSUE,
* ORREFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVERTO
*MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* Thefollowing software/firmware and/or related documentation ("MediaTekSoftware")
* havebeen modified by MediaTek Inc. All revisions are subject to anyreceiver‘s
*applicable license agreements with MediaTek Inc.
*/
/*Copyright Statement:
*
* Thissoftware/firmware and related documentation ("MediaTekSoftware") are
*protected under relevant copyright laws. The information containedherein
* isconfidential and proprietary to MediaTek Inc. and/or its licensors.
*Without the prior written permission of MediaTek inc. and/or itslicensors,
* anyreproduction, modification, use or disclosure of MediaTek Software,
* andinformation contained herein, in whole or in part, shall be strictlyprohibited.
*/
/*MediaTek Inc. (C) 2010. All rights reserved.
*
* BYOPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES ANDAGREES
* THATTHE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEKSOFTWARE")
*RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TORECEIVER ON
* AN"AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY ANDALL WARRANTIES,
*EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIEDWARRANTIES OF
*MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TOTHE
*SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLYTO SUCH
* THIRDPARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLYACKNOWLEDGES
* THATIT IS RECEIVER‘S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTYALL PROPER LICENSES
*CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BERESPONSIBLE FOR ANY MEDIATEK
*SOFTWARE RELEASES MADE TO RECEIVER‘S SPECIFICATION OR TO CONFORM TO APARTICULAR
*STANDARD OR OPEN FORUM. RECEIVER‘S SOLE AND EXCLUSIVE REMEDY ANDMEDIATEK‘S ENTIRE AND
*CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASEDHEREUNDER WILL BE,
* ATMEDIATEK‘S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE ATISSUE,
* ORREFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVERTO
*MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* Thefollowing software/firmware and/or related documentation ("MediaTekSoftware")
* havebeen modified by MediaTek Inc. All revisions are subject to anyreceiver‘s
*applicable license agreements with MediaTek Inc.
*/
packagecom.mediatek.engineermode.io;
publicclass EmGpio {
publicstatic native int getGpioMaxNumber();
publicstatic native boolean gpioInit();
publicstatic native boolean gpioUnInit();
publicstatic native boolean setGpioInput(int gpioIndex);
publicstatic native boolean setGpioOutput(int gpioIndex);
publicstatic native boolean setGpioDataHigh(int gpioIndex);
publicstatic native boolean setGpioDataLow(int gpioIndex);
publicstatic native int getCurrent(int hostNumber);
publicstatic native int newGetCurrent(int hostNumber, int opcode);
publicstatic native boolean setCurrent(int hostNumber, int currentDataIdx,
intcurrentCmdIdx);
publicstatic native boolean newSetCurrent(int hostNumber, int clkpu,
intclkpd, int cmdpu, int cmdpd, int datapu, int datapd,
inthopbit, int hoptime, int opcode);
publicstatic native boolean setSd30Mode(int hostNumber, int sd30Mode,
intsd30MaxCurrent, int sd30Drive, int sd30PowerControl);
static{
System.loadLibrary("em_gpio_jni");
}
}
注意:路径和类名不能修改。
下面调试及可。
mtk硬件启动关闭蓝牙功能的项目:mtk 上层操作GPIO应用示例
标签:mtk gpio bluetooth-s sharedpreferences
原文地址:http://blog.csdn.net/zangcf/article/details/44499219