2016-05-12 5 views
0

Verfahren getSystemService(String) für den Typ nicht definiert ist BluetoothDeviceandroid Bluetooth: Die Methode getSystemService (String) ist nicht definiert für den Typ BluetoothDevice

DER KLASSE TelephonyInfo, dass meine SIM-Karten auf meinem Handy greift arbeitet

Ich möchte um es zu verwenden, um die Simdetails auf einem Gerät anzuzeigen, mit dem ich verbunden bin, machte ich einige Änderungen, um Bluetooth einzuschließen, aber ich habe ein Problem mit der methodgetSystemService Die Methode getSystemService(String) ist undefined für den Typ BluetoothDevice, fand ich einiges ähnliche Fragen, aber keine benutzten Bluetooth, ich versuchte ihre Lösungen, aber es hat nicht funktioniert, seit ich Bluetooth verwende. Hier ist der Code, nachdem ich einige Änderungen vorgenommen habe

ich diese Klasse bin mit den SIM-Details von einem Bluetooth-Gerät zu zeigen, ich bin verbunden, ich habe die Klasse TelephonyInfo

public final class TelephonyInfo { 

    private static TelephonyInfo telephonyInfo; 
    private String imeiSIM1; 
    private String imeiSIM2; 
    private boolean isSIM1Ready; 
    private boolean isSIM2Ready; 

    public String getImeiSIM1() { 
     return imeiSIM1; 
    } 

     /*public static void setImeiSIM1(String imeiSIM1) { 
     TelephonyInfo.imeiSIM1 = imeiSIM1; 
      }*/ 

    public String getImeiSIM2() { 
     return imeiSIM2; 
    } 

    /*public static void setImeiSIM2(String imeiSIM2) { 
    TelephonyInfo.imeiSIM2 = imeiSIM2; 
    }*/ 

    public boolean isSIM1Ready() { 
     return isSIM1Ready; 
    } 

    /*public static void setSIM1Ready(boolean isSIM1Ready) { 
    TelephonyInfo.isSIM1Ready = isSIM1Ready; 
     }*/ 

    public boolean isSIM2Ready() { 
     return isSIM2Ready; 
    } 

    /*public static void setSIM2Ready(boolean isSIM2Ready) { 
    TelephonyInfo.isSIM2Ready = isSIM2Ready; 
    }*/ 

    public boolean isDualSIM() { 
     return imeiSIM2 != null; 
    } 

    private TelephonyInfo() { 
    } 

    public static TelephonyInfo getInstance(BluetoothDevice bluetoothDevice){ 

     if(telephonyInfo == null) { 

      telephonyInfo = new TelephonyInfo(); 

      TelephonyManager telephonyManager = ((TelephonyManager) 
        bluetoothDevice.getSystemService(Context.TELEPHONY_SERVICE)); 

      telephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();; 
      telephonyInfo.imeiSIM2 = null; 

      try { 
       telephonyInfo.imeiSIM1 = getDeviceIdBySlot(bluetoothDevice, 
         "getDeviceIdGemini", 0); 
       telephonyInfo.imeiSIM2 = getDeviceIdBySlot(bluetoothDevice, 
         "getDeviceIdGemini", 1); 
      } catch (GeminiMethodNotFoundException e) { 
       e.printStackTrace(); 

       try { 
        telephonyInfo.imeiSIM1 = getDeviceIdBySlot(bluetoothDevice, 
          "getDeviceId", 0); 
        telephonyInfo.imeiSIM2 = getDeviceIdBySlot(bluetoothDevice, 
          "getDeviceId", 1); 
       } catch (GeminiMethodNotFoundException e1) { 
        //Call here for next manufacturer's predicted method name if you  
        wish 
        e1.printStackTrace(); 
       } 
      } 

      telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == 
        TelephonyManager.SIM_STATE_READY; 
      telephonyInfo.isSIM2Ready = false; 

      try { 
       telephonyInfo.isSIM1Ready = getSIMStateBySlot(bluetoothDevice, 
         "getSimStateGemini", 0); 
       telephonyInfo.isSIM2Ready = getSIMStateBySlot(bluetoothDevice, 
         "getSimStateGemini", 1); 
      } catch (GeminiMethodNotFoundException e) { 

       e.printStackTrace(); 

       try { 
        telephonyInfo.isSIM1Ready = getSIMStateBySlot(bluetoothDevice, 
          "getSimState", 0); 
        telephonyInfo.isSIM2Ready = getSIMStateBySlot(bluetoothDevice, 
          "getSimState", 1); 
       } catch (GeminiMethodNotFoundException e1) { 
        //Call here for next manufacturer's predicted method name if you 
        wish 
        e1.printStackTrace(); 
       } 
      } 
     } 

     return telephonyInfo; 
    } 

    private static String getDeviceIdBySlot(BluetoothDevice bluetoothDevice, 
              String predictedMethodName, int slotID) throws 
      GeminiMethodNotFoundException { 

     String imei = null; 

     TelephonyManager telephony = (TelephonyManager) 
       bluetoothDevice.getSystemService(Context.TELEPHONY_SERVICE); 

     try{ 

      Class<?> telephonyClass = Class.forName(telephony.getClass().getName()); 

      Class<?>[] parameter = new Class[1]; 
      parameter[0] = int.class; 
      Method getSimID = telephonyClass.getMethod(predictedMethodName, 
        parameter); 

      Object[] obParameter = new Object[1]; 
      obParameter[0] = slotID; 
      Object ob_phone = getSimID.invoke(telephony, obParameter); 

      if(ob_phone != null){ 
       imei = ob_phone.toString(); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new GeminiMethodNotFoundException(predictedMethodName); 
     } 

     return imei; 
    } 

    private static boolean getSIMStateBySlot(BluetoothDevice bluetoothDevice, 
               String predictedMethodName, int slotID) throws 
      GeminiMethodNotFoundException { 

     boolean isReady = false; 

     TelephonyManager telephony = (TelephonyManager) 
       bluetoothDevice.getSystemService(Context.TELEPHONY_SERVICE); 

     try{ 

      Class<?> telephonyClass = Class.forName(telephony.getClass().getName()); 

      Class<?>[] parameter = new Class[1]; 
      parameter[0] = int.class; 
      Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName, 
        parameter); 

      Object[] obParameter = new Object[1]; 
      obParameter[0] = slotID; 
      Object ob_phone = getSimStateGemini.invoke(telephony, obParameter); 

      if(ob_phone != null){ 
       int simState = Integer.parseInt(ob_phone.toString()); 
       if(simState == TelephonyManager.SIM_STATE_READY){ 
        isReady = true; 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new GeminiMethodNotFoundException(predictedMethodName); 
     } 

     return isReady; 
    } 


    private static class GeminiMethodNotFoundException extends Exception { 

     private static final long serialVersionUID = -996812356902545308L; 

     public GeminiMethodNotFoundException(String info) { 
      super(info); 
     } 
    } 


} 

Kann jemand bitte Helfen Sie mir, dieses Problem zu lösen?

+0

versuchen mit this.getSystemService (String) –

+0

das ist, wo das Problem Sathish Kumar J, die Funktion getSystemService (String) für bluetoothDevice nicht definiert ist, ich mich gefragt, ob es eine ähnliche Funktion, aber kompatibel mit Bluetooth-Gerät – xxxx

+0

und wenn i benutze dies anstelle von bluetoothDevice, es wird nicht auf Bluetooth-Gerät angewendet, aber trotzdem bekomme ich diesen Fehler Kann nicht in einem statischen Kontext verwenden, danke – xxxx

Antwort

0

getSystemService() ist eine Methode der Klasse Context, so dass Sie es auf einem Kontext zu laufen brauchen.

Der ursprüngliche Code Sie es kopiert wurde wahrscheinlich von einer Aktivität abgeleiteten Klasse lief gemeint sein. Sie müssen ein Context Argument in Ihre Methode übergeben, wenn es sich nicht in einer Aktivität befindet.

+0

Ja, ich denke schon, es tut mir leid, ich bin ein bisschen neu in diesem Ich möchte, dass der Code mit einem Bluetooth-Gerät verwendet werden kann (das heißt, die Sim-Details auf einem Gerät erhalten, mit dem ich über Bluetooth verbunden bin). Ich habe versucht, einige Änderungen am Originalcode vorzunehmen: hier ist der erste Code http: // fr.androids .help/q15785, bitte helft, was kann ich ändern? Danke – xxxx

Verwandte Themen