1. 程式人生 > >mtk硬體啟動關閉藍芽功能的專案:mtk 上層操作GPIO應用示例

mtk硬體啟動關閉藍芽功能的專案:mtk 上層操作GPIO應用示例

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處理程式中,檢查btswitched的狀態,如果為//checked狀態,那麼開啟藍芽,如果為非checked狀態,則不理會。

第一步,在AndroidManifest.xml中新增:

<receiverandroid:name="BootCompletedReceiver">

<intent-filter>

<actionandroid:name="android.intent.action.BOOT_COMPLETED"

/>

</intent-filter>

</receiver>

不要忘記加入許可權:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

另外,接收BOOT_COMPLETED,不能安裝,必須直接push到/system/app/

在當前包下面建立BootCompletedReceiver.java檔案

我們定義兩個SharedPreferences來儲存系統的btethernet狀態,因為省電需求,我們每次開機都保持EthernetBT為關閉。

package

com.mediatek.oobe;

importandroid.app.Activity;

importandroid.content.BroadcastReceiver;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.content.SharedPreferences;

importandroid.util.Log;

publicclassBootCompletedReceiverextendsBroadcastReceiver {

@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類裡面增加三個私有成員

privateSharedPreferencesbtSwitchSharedPreferences;

privateStringbtSwitchStatusString;

privateSharedPreferences.EditormEditor;

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, INCO