Skip to content

Commit 9606282

Browse files
committed
add some sample code from @D3VL. actually the issue is my cable not the code :(
1 parent fb77e3c commit 9606282

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

app/src/main/AndroidManifest.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
<category android:name="android.intent.category.LAUNCHER" />
1919
</intent-filter>
20-
<intent-filter>
21-
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
22-
</intent-filter>
20+
<!-- <intent-filter>-->
21+
<!-- <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />-->
22+
<!-- </intent-filter>-->
2323

24-
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
25-
android:resource="@xml/device_filter" />
24+
<!-- <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"-->
25+
<!-- android:resource="@xml/device_filter" />-->
2626
</activity>
2727
</application>
2828
<uses-feature android:name="android.hardware.usb.host" />

app/src/main/java/com/example/ijdfpvviewer/MainActivity.java

+32-5
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@
2525
import android.os.Message;
2626
import android.util.Log;
2727
import android.view.SurfaceView;
28-
import android.view.View;
2928
import android.widget.Button;
3029
import android.widget.Toast;
3130

3231
import java.util.ArrayList;
3332
import java.util.Arrays;
33+
import java.util.HashMap;
3434
import java.util.List;
3535

3636

3737
public class MainActivity extends AppCompatActivity {
3838

39+
private static final String ACTION_USB_PERMISSION = "ACTION_USB_PERMISSION";
3940
UsbManager mUsbManager;
4041
UsbDevice device;
4142
boolean usbConnected;
@@ -75,13 +76,17 @@ public void handleMessage(Message msg) {
7576
};
7677

7778
connectButton.setOnClickListener(vw -> {
78-
if(device == null) {
79+
if(checkMyDevice()){
80+
81+
};
82+
83+
/*if(device == null) {
7984
Log.d("DEVICE", "no device !");
8085
Toast.makeText(getApplicationContext(), "no device !", Toast.LENGTH_SHORT).show();
8186
}else{
8287
Log.d("DEVICE", device.getDeviceName() + " " +device.getManufacturerName()+ " " +device.getProductName());
8388
Toast.makeText(getApplicationContext(), device.getDeviceName() + " " +device.getManufacturerName()+ " " +device.getProductName(), Toast.LENGTH_SHORT).show();
84-
}
89+
}*/
8590
});
8691
Log.d("INITIALIZED", "app running !");
8792

@@ -103,6 +108,30 @@ protected void displayFrame(SurfaceView v, Bitmap f, Rect zone){
103108
}
104109
}
105110

111+
private boolean checkMyDevice() {
112+
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
113+
Log.d("USB_SIZE", ("usbdevice size=" + deviceList.size() + ""));
114+
if (deviceList.size() <= 0) {
115+
device = null;
116+
Toast.makeText(getApplicationContext(), "no device !", Toast.LENGTH_SHORT).show();
117+
118+
return false;
119+
}
120+
for (UsbDevice d : deviceList.values()) {
121+
Log.d("USB_DEVICE","VID=" + d.getVendorId() + " PID=" + d.getProductId() + "");
122+
Toast.makeText(getApplicationContext(), d.getDeviceName() + " " +d.getManufacturerName()+ " " +d.getProductName(), Toast.LENGTH_SHORT).show();
123+
124+
if (d.getVendorId() == 11427 && d.getProductId() == 31) {
125+
device = d;
126+
if (mUsbManager.hasPermission(device)) {
127+
return true;
128+
} else Log.d("USB_PERM:","No Permission");
129+
mUsbManager.requestPermission(d, PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(ACTION_USB_PERMISSION), 0));
130+
}
131+
}
132+
return false;
133+
}
134+
106135
@Override
107136
protected void onCreate(Bundle savedInstanceState) {
108137
super.onCreate(savedInstanceState);
@@ -180,8 +209,6 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
180209
}
181210
}
182211

183-
private static final String ACTION_USB_PERMISSION =
184-
"com.android.example.USB_PERMISSION";
185212
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
186213

187214
public void onReceive(Context context, Intent intent) {

0 commit comments

Comments
 (0)