Recent research in FM mode, FM is a value I'm learning module. What can be seen from the top.
The upper layer is the FM button operation and interface display, so call to FM to achieve the function of radio listening.
Look at the FM start-up process: for example:
First Enter the Fmradio.java class and OnCreate initialize some data. Draw the FM interface. Start the FM in the OnStart () method to start Fmradioservice.java (call the Bindtoservice (this, OSC) method).
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgzzbg92zxhpemk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Register under FM settings (send a set broadcast after Setup, update the status of the Fmradio Class).
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgzzbg92zxhpemk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Load initialization data, get frequency address
Newpresetstation ("", Fmsharedpreferences.gettunedfrequency ());
In the Bindtoservice (This,osc) method, first start the StartService (the same service is only oncreate once), Restart the Bindservice (this has the advantage of pressing the return key service does not go OnDestroy method) Bindservice through Onbind callback a IBinder object to Fmradio class of serviceconnection of the inner class Onserviceco Nnected method, call the Enableradio () method.
Call Fmradio.java's Isantennaavailable () method in the Enableraido method for headphone inference, whether the antenna inference is available. Controlled by a plug-in pull-out broadcast receive (Registerheadsetlistener () method in Fmradio) action (Intent.action_headset_plug)
mheadsetplugged = (Intent.getintextra ("state", 0) = = 1); Equals 1 indicates the headset is available and equals 0 is available.
Call Fmradio Method Fmon (Mservice.fmon ())
Interface available Enableradioonoffui ()
<span style= "FONT-SIZE:18PX;" >private void Enableradio () {misscaning = false; Misseeking = false; Missearching = false; Boolean bstatus = false; if (Ishdmion ()) {ShowDialog (dialog_cmd_failed_hdmi_on); }else {<span style= "font-family:kaiti_gb2312;" > </span>if (mservice! = null) {try {if (false = = Mservice.isfmon ()) && < ; strong>isantennaavailable () </strong>) {bstatus = Mservice.fmon (); if (bstatus) {Tuneradio (fmsharedpreferences.gettunedfrequency ()); <strong> Enableradioonoffui ();</strong>}else {log.e (LogTag, "Mservice.fmon failed"); mcommandfailed = Cmd_fmon; if (iscallactive ()) {Enableradioonoffui (); ShowDialog (dialog_cmd_failed_call_on); }else { ShowDialog (dialog_cmd_failed); }}}else {Enableradioonoffui (); }}catch (RemoteException e) {e.printstacktrace (); }}}}</span>
Initialize the Fmreceiver reference mreceiver = Newfmreceiver (fmradio_device_fd_string, FmCallbacks) in the Fmradioservice.java Fmon () method;
Remove settings Save the region frequency Property fmconfig config =fmsharedpreferences.getfmconfiguration ();
Really accept FM sound in Bstatus =mreceiver.enable (Fmsharedpreferences.getfmconfiguration ());
Isspeakerenabled () speaker available, user-set speaker
/* * Turn on FM:POWERS up FM hardware, and initializes the FM module * . * @return TRUE if FM Enable API was invoked successfully, false if the API failed. */Private Boolean Fmon () {Boolean bstatus=false; Mwakelock.acquire (10*1000); if (Telephonymanager.call_state_idle! = Getcallstate ()) {return bstatus; } if (mreceiver = = null) {try {<strong> mreceiver = new Fmreceiver (fmradio_device_fd_str ING, fmcallbacks);</strong>} catch (Instantiationexception e) {throw new Runtim Eexception ("Fmreceiver Service not available!"); }} if (mreceiver! = null) {if (Isfmon ()) {/* FM is already on,*/BS Tatus = true; LOG.D (LogTag, "Mreceiver.already enabled"); } else {//This sets up the FM radio device Fmconfig config = fmsharedpreferences.getfmconfiguration (); LOG.D (LogTag, "Fmon:radioband:" + Config.getradioband ()); LOG.D (LogTag, "fmon:emphasis:" + config.getemphasis ()); LOG.D (LogTag, "fmon:chspacing:" + config.getchspacing ()); LOG.D (LogTag, "FMON:RDSSTD:" + CONFIG.GETRDSSTD ()); LOG.D (LogTag, "fmon:lowerlimit:" + config.getlowerlimit ()); LOG.D (LogTag, "fmon:upperlimit:" + config.getupperlimit ()); <strong> bstatus = mreceiver.enable (Fmsharedpreferences.getfmconfiguration ());</strong> if (isSpeak Erenabled ()) {Setaudiopath (false); } else {Setaudiopath (true); } log.d (LogTag, "mreceiver.enable done, Status:" + bstatus); if (bstatus = = true) {/* Put the hardware into normal mode */<strong> Bstat US = Setlowpowermode (false);</strong> log.d (LogTag, "sEtlowpowermode done, Status: "+ bstatus); Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service); if (Audiomanager! = null) && (false = = mplaybackinprogress)) {LOG.D (LogTag, "Maudiomana Ger.setfmradioon = true \ n "); Audiomanager.setparameters ("fmradioon=" +maudiodevice); <strong>int state = Getcallstate ();</strong> if (telephonymanager.call_state_idle! = getCallSt Ate ()) {<strong>fmactiononcallstate (state);</strong>} else { <span style= "color: #00CCCC;" ><strong> STARTFM ();</strong> </span>//enable FM Audio only if call is IDLE} LOG.D (LogTag, "Maudiomanager.setfmradioon done \ n"); }if (Mreceiver! = null) {//<span style= "font-family:kaiti_gb2312;font-size:18px;" > Manual Remote Group Processing </span><span style= "font-family:kaiti_gb2312; " > </span> <strong>bstatus = mreceiver.registerrdsgroupprocessing (FmReceiver.FM_RX_RDS_GRP_ rt_ebl| fmreceiver.fm_rx_rds_grp_ps_ebl| fmreceiver.fm_rx_rds_grp_af_ebl| FMRECEIVER.FM_RX_RDS_GRP_PS_SIMPLE_EBL);</strong> log.d (LogTag, "registerrdsgroupprocessing done, Sta Tus: "+ bstatus); } <strong>bstatus = Enableautoaf (Fmsharedpreferences.getautoafswitch ()); </strong>//<span style= " font-family:kaiti_gb2312;font-size:18px; " > can take the initiative to jump to the selected frequency </span> log.d (LogTag, "Enableautoaf done, Status:" + bstatus); /* There is no internal antenna*/<strong>bstatus = Mreceiver.setinternalantenna (false);/</strong>/ <span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;"> Set the built-in antenna to 0</span> LOG.D (LogTag," Setinternalantenna done, Status: "+ bstatus); /* Read back to verify the internal antenna mode*/readinternalantennaavailable (); Startnotification (); Bstatus = true; } else {mreceiver = null;//As Enable failed no need to disable//failure o F enable can be because handle//already open which gets effected if We disable stop (); }} return (Bstatus); }
Set Ringtone Path boolean state =mreceiver.setanalogmode (Analogmode);
Private Boolean Setaudiopath (Boolean analogmode) { if (mreceiver = = null) { return false; } if (isanalogmodeenabled () = = Analogmode) { log.d (LogTag, "Analog Path already is set to" +analogmode); return false; } if (!isanalogmodesupported ()) { log.d (LogTag, "Analog Path is not supported"); return false; } if (Systemproperties.getboolean ("Hw.fm.digitalpath", False)) { return false; } Boolean State =<strong> Mreceiver.setanalogmode (analogmode);</strong> if (false = = state) { LOG.D (LogTag, "Error in toggling analog/digital path" + analogmode); return false; } misanalogpathenabled = Analogmode; return true; }
Analogmode analog Set low power bstatus = Setlowpowermode (false);
The phone is not in the idle shape too under int state = Getcallstate ();
Fmactiononcallstate (state);
Activate FM STARTFM ();
private void Startfm () {LOG.D (LogTag, "in Startfm"); if (true = = Mappshutdown) {//not-to-send intent to Audiomanager in Shutdown return; } if (Iscallactive ()) {//When call was active never let audio playback Mresumeaftercall = true; Return } Mresumeaftercall = false; if (true = = mplaybackinprogress)//No need to resend event return; Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service); int granted = Audiomanager.requestaudiofocus (Maudiofocuslistener, Audiomanager.stream_music, AudioManager.AUD Iofocus_gain_transient); if (granted! = audiomanager.audiofocus_request_granted) {log.d (LogTag, "audio Focuss couldnot be granted"); Return } log.d (LogTag, "FM Registering for Registermediabuttoneventreceiver"); Maudiomanager = (Audiomanager) getsystemservice (Context.audio_service); ComponentName Fmradio = new ComponentName (This.getpackagename (), FMMediaButtonIntentReceiver.class.getName ()) ; Maudiomanager.registermediabuttoneventreceiver (Fmradio); Mstoppedonfocusloss = false; if (!isspeakerenabled () &&!ma2dpdevicesupportinhal && (true = = Ma2dpdevicestate.isdeviceavailable ()) &&!isanalogmodeenabled () && (true = = Starta2dpplayback ())) {MOVERA2DP=TR Ue LOG.D (LogTag, "Audio source set it as A2DP"); <strong> Audiosystem.setforceuse (Audiosystem.for_media, AUDIOSYSTEM.FORCE_BT_A2DP);</strong>} else { LOG.D (LogTag, "fmradio:requesting to start FM"); Reason for resending the Speaker option is we am sending//action_fm=1 to Audiomanager, the previous state of Speaker we set//need not being retained by the Audio Manager. <strong>audiosystem.setdeviceconnectionstate (Audiosystem.deviCE_OUT_FM, Audiosystem.device_state_available, ""); </strong>//<span style= "Font-fam ily:kaiti_gb2312;font-size:18px; " >FM device </span> if (isspeakerenabled ()) {Mspeakerphoneon = true; LOG.D (LogTag, "Audio source set it as Speaker"); <strong> Audiosystem.setforceuse (Audiosystem.for_media, Audiosystem.force_speaker);</strong>} else {LOG.D (LogTag, "Audio source set it as headset"); <strong> Audiosystem.setforceuse (Audiosystem.for_media, Audiosystem.force_none);</strong>}} Sendrecordserviceintent (Record_start); Mplaybackinprogress = true; }
Set up headphones to accept FM sound
Audiosystem.setforceuse (Audiosystem.for_media,audiosystem.force_none);
FM devices available audiosystem.setdeviceconnectionstate (AUDIOSYSTEM.DEVICE_OUT_FM,
Audiosystem.device_state_available, "");
Manual Remote Group Processing
Bstatus = mreceiver.registerrdsgroupprocessing (fmreceiver.fm_rx_rds_grp_rt_ebl|
fmreceiver.fm_rx_rds_grp_ps_ebl|
fmreceiver.fm_rx_rds_grp_af_ebl|
FMRECEIVER.FM_RX_RDS_GRP_PS_SIMPLE_EBL);
You can actively jump to the selected frequency Bstatus =enableautoaf (Fmsharedpreferences.getautoafswitch ());
Set the built-in antenna to 0 Fmtransceiver.java
Mreceiver.setinternalantenna (False)
Fmreceiverjni.setcontrolnative (SFd, V4l2_cid_private_tavarua_antenna,iantenna)
<span Style= "FONT-SIZE:18PX;" >/*============================================================== Function:setinternalantenna =============== ===============================================*//** * Returns True if successful, false otherwise * * < P> * This method sets internal antenna type to TRUE/FALSE * * @param intantenna True are internal antenna is Present * * <p> * @return True/false */Public Boolean Setinternalantenna (Boolean intant) { int Iantenna; if (intant) Iantenna = 1; else Iantenna = 0; int re = <strong>fmreceiverjni.setcontrolnative (SFd, V4l2_cid_private_tavarua_antenna, IAntenna); </strong > if (re = = 0) return true; return false; }</span>
OK, so far, the FM start-up work is basically over. The next step is to search for the channel and maybe continue to analyze the FM search.
Module FM boot process in Android FM learning