[英]how to detect device connect on android?
我想使用Android上的應用程序檢測連接的設備。
我想檢測鍵盤,鼠標和閃存驅動器。
我目前正在使用hwinfo
命令和計時器
//keyboard detect class.
public class detectService extends Service {
static Process hwinfo;
static String keyboard = "";
private Handler handler;
private Timer timer;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
String[] cmd = new String[] {"su", "-c", "hwinfo --keyboard | grep - i 'keyboard'"};
try {
hwinfo = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(hwinfo.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
line = line.trim().toLowerCase();
keyboard = line;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
if (keyboard.contains("keyboard")) {
timer.cancel();
Intent intent = new Intent(this, keyboardDialog.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
}
};
timer = new Timer("Service");
timer.scheduleAtFixedRate(timerTask, 0 , 4000);
}
此源代碼可以很好地檢測鍵盤。 但是它每4秒執行一次。
連接鍵盤,鼠標和閃存驅動器時,我不想使用計時器。
檢測在android上連接的設備。
事件檢測或接收器。
如何不使用Timer來檢測android上連接的設備?
我將說明USB設備檢測代碼。 希望會對您有所幫助。
第1步: 請求訪問USB端口的許可,這在清單文件中完成,例如:
<uses-feature android:name="android.hardware.usb.host" />
步驟2: 清單文件中的USB配置
<activity
android:name="yourPackageName.MainActivity"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data android:name=
"android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
<meta-data android:name=
"android.hardware.usb.action.USB_DEVICE_DETACHED"
android:resource="@xml/device_filter" />
</activity>
步驟3: 在res / xml下創建文件,並將其命名為device_filter
將以下代碼粘貼到其中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" />
<!-- 0x2341 / Arduino -->
<usb-device vendor-id="9025" />
</resources>
步驟4: 連接到USB設備的安全性和用戶權限
由於我們的應用程序不知道用戶是否已經授予了權限,因此每次我們要啟動USB連接時,我們都需要始終檢查用戶權限標志:
void checkUSB(){
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
// Get the list of attached devices
HashMap<String, UsbDevice> devices = manager.getDeviceList();
// Iterate over all devices
Iterator<String> it = devices.keySet().iterator();
while (it.hasNext()) {
String deviceName = it.next();
UsbDevice device = devices.get(deviceName);
String VID = Integer.toHexString(device.getVendorId()).toUpperCase();
String PID = Integer.toHexString(device.getProductId()).toUpperCase();
if (!manager.hasPermission(device)) {
private static final String ACTION_USB_PERMISSION = "yourPackageName.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
manager.requestPermission(device, mPermissionIntent);
return;
} else {
... //user permission already granted; prceed to access USB device
}
}
}
而已!! 快樂編碼:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.