2016-05-12 74 views
0

工作Android的蓝牙:该方法getSystemService(字符串)是未定义的方法<code>getSystemService(String)</code>是未定义类型<code>BluetoothDevice</code></p> <p>访问我的手机在我的SIM卡类<code>TelephonyInfo</code>类型BluetoothDevice类

我想使用它来显示我连接到的设备上的SIM卡细节,我做了一些更改,以包括蓝牙,但我遇到了问题methodgetSystemService 方法getSystemService(String)未定义类型BluetoothDevice, 我发现一些类似的问题,但没有人使用蓝牙,我尝试了他们的解决方案,但它没有工作,因为我使用蓝牙。下面的代码,我做了一些改动

后我使用这个类来说明蓝牙设备我连接,我有类的辛细节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); 
     } 
    } 


} 

可有人请帮我解决这个问题?

+0

尝试使用this.getSystemService(字符串) –

+0

这就是问题的地方Sathish Kumar J,函数getSystemService(String)未定义为bluetoothDevice,我想知道是否有类似的功能,但与蓝牙设备 – xxxx

+0

兼容,如果我使用这个而不是bluetoothDevice,它不会被应用在蓝牙设备上,但无论如何,我得到这个错误不能在静态上下文中使用这个,谢谢 – xxxx

回答

0

getSystemService()是类语境的方法,所以你需要在一个环境中运行它。

你很可能意味着它复制原始代码从一个活动派生类中运行。如果不在Activity中,则需要将上下文参数传递到您的方法中。

+0

是的,我想是的,我很抱歉,我有点新,我希望代码可以与蓝牙设备一起使用(意思是通过蓝牙连接到设备上的sim细节)我尝试对原始代码进行一些更改:这里是第一个代码http://fr.androids .help/q15785,请帮忙,我可以做些什么改变?谢谢 – xxxx

相关问题