Editing GRAndWalkthroughUSRP

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 31: Line 31:
 
First, add these variables to your MainActivity class:
 
First, add these variables to your MainActivity class:
  
<syntaxhighlight lang="Java">    public static Intent intent = null;
+
<pre>    public static Intent intent = null;
 
     private String usbfs_path = null;
 
     private String usbfs_path = null;
 
     private int fd = -1;
 
     private int fd = -1;
     private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
+
     private static final String ACTION_USB_PERMISSION = &quot;com.android.example.USB_PERMISSION&quot;;
     private static final String DEFAULT_USBFS_PATH = "/dev/bus/usb";
+
     private static final String DEFAULT_USBFS_PATH = &quot;/dev/bus/usb&quot;;
 
     private UsbManager mUsbManager;
 
     private UsbManager mUsbManager;
     private UsbDevice mUsbDevice;</syntaxhighlight>
+
     private UsbDevice mUsbDevice;</pre>
 
We need to make sure we have the USB permissions set before we can launch the radio. So instead of doing everything in '''OnCreate''' like we've been doing, we'll separate into a few different functions. First, in '''OnCreate''', we need to get the USB device, set up a broadcast receiver, and then ask the user for permission to use the device. When the user grants permission, the broadcast receiver is launched. The broadcast receiver finishes up getting access to the USB device, and now we can act on the USB device to get the information out of it we need. This is what our '''OnCreate''' function looks like now:
 
We need to make sure we have the USB permissions set before we can launch the radio. So instead of doing everything in '''OnCreate''' like we've been doing, we'll separate into a few different functions. First, in '''OnCreate''', we need to get the USB device, set up a broadcast receiver, and then ask the user for permission to use the device. When the user grants permission, the broadcast receiver is launched. The broadcast receiver finishes up getting access to the USB device, and now we can act on the USB device to get the information out of it we need. This is what our '''OnCreate''' function looks like now:
  
<syntaxhighlight lang="Java">    @Override
+
<pre>    @Override
 
     protected void onCreate(Bundle savedInstanceState) {
 
     protected void onCreate(Bundle savedInstanceState) {
 
         super.onCreate(savedInstanceState);
 
         super.onCreate(savedInstanceState);
Line 77: Line 77:
 
         mUsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
 
         mUsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
 
         if (mUsbDevice == null) {
 
         if (mUsbDevice == null) {
             Log.d("GrTemplateUSRP", "Didn't get a device; finding it now.");
+
             Log.d(&quot;GrTemplateUSRP&quot;, &quot;Didn't get a device; finding it now.&quot;);
 
             final HashSet allowed_devices = getAllowedDevices(this);
 
             final HashSet allowed_devices = getAllowedDevices(this);
 
             final HashMap usb_device_list = mUsbManager.getDeviceList();
 
             final HashMap usb_device_list = mUsbManager.getDeviceList();
 
             for (UsbDevice candidate : usb_device_list.values()) {
 
             for (UsbDevice candidate : usb_device_list.values()) {
                 String candstr = "v" + candidate.getVendorId() + "p" + candidate.getProductId();
+
                 String candstr = &quot;v&quot; + candidate.getVendorId() + &quot;p&quot; + candidate.getProductId();
 
                 if (allowed_devices.contains(candstr)) {
 
                 if (allowed_devices.contains(candstr)) {
 
                     // Need to handle case where we have more than one device connected
 
                     // Need to handle case where we have more than one device connected
Line 88: Line 88:
 
             }
 
             }
 
         }
 
         }
         Log.d("GrTemplateUSRP", "Selected Device: " + mUsbDevice);
+
         Log.d(&quot;GrTemplateUSRP&quot;, &quot;Selected Device: &quot; + mUsbDevice);
 
         PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0,
 
         PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0,
 
                 new Intent(ACTION_USB_PERMISSION), 0);
 
                 new Intent(ACTION_USB_PERMISSION), 0);
Line 95: Line 95:
 
         // If use hits OK, the broadcast receiver will be launched.
 
         // If use hits OK, the broadcast receiver will be launched.
 
         mUsbManager.requestPermission(mUsbDevice, permissionIntent);
 
         mUsbManager.requestPermission(mUsbDevice, permissionIntent);
     }</syntaxhighlight>
+
     }</pre>
 
Notice that we're still creating the Seek Bar here as the GUI setup.
 
Notice that we're still creating the Seek Bar here as the GUI setup.
  
Line 103: Line 103:
  
 
<pre>
 
<pre>
<resources>
+
     
    <usb-device vendor-id="3034" product-id="10296" />  <!-- RTL-SDR -->
+
         
    <usb-device vendor-id="9472" product-id="2" />      <!-- USRP B100 -->
+
       
    <usb-device vendor-id="9472" product-id="32" />    <!-- USRP B200/B210 -->
+
       
    <usb-device vendor-id="9472" product-id="33" />    <!-- USRP B200/B210 -->
+
       
    <usb-device vendor-id="9472" product-id="34" />    <!-- USRP B200mini -->
+
   
    <usb-device vendor-id="14627" product-id="30739" /> <!-- NI B200/B210 -->
+
   
    <usb-device vendor-id="14627" product-id="30740" /> <!-- NI B200/B210 -->
 
</resources>
 
 
</pre>
 
</pre>
 
We may have to add more devices here for other hardware support in the future.
 
We may have to add more devices here for other hardware support in the future.
Line 117: Line 115:
 
And this is what '''getAllowedDevices''' looks like:
 
And this is what '''getAllowedDevices''' looks like:
  
<syntaxhighlight lang="Java">    private static HashSet getAllowedDevices(final Context ctx) {
+
<pre>    private static HashSet getAllowedDevices(final Context ctx) {
 
         final HashSet ans = new HashSet();
 
         final HashSet ans = new HashSet();
 
         try {
 
         try {
Line 128: Line 126:
 
                 switch (eventType) {
 
                 switch (eventType) {
 
                     case XmlPullParser.START_TAG:
 
                     case XmlPullParser.START_TAG:
                         if (xml.getName().equals("usb-device")) {
+
                         if (xml.getName().equals(&quot;usb-device&quot;)) {
 
                             final AttributeSet as = Xml.asAttributeSet(xml);
 
                             final AttributeSet as = Xml.asAttributeSet(xml);
                             final Integer vendorId = Integer.valueOf(as.getAttributeValue(null, "vendor-id"), 10);
+
                             final Integer vendorId = Integer.valueOf(as.getAttributeValue(null, &quot;vendor-id&quot;), 10);
                             final Integer productId = Integer.valueOf(as.getAttributeValue(null, "product-id"), 10);
+
                             final Integer productId = Integer.valueOf(as.getAttributeValue(null, &quot;product-id&quot;), 10);
                             ans.add("v" + vendorId + "p" + productId);
+
                             ans.add(&quot;v&quot; + vendorId + &quot;p&quot; + productId);
 
                         }
 
                         }
 
                         break;
 
                         break;
Line 143: Line 141:
  
 
         return ans;
 
         return ans;
     }</syntaxhighlight>
+
     }</pre>
 
The broadcast receiver (See the main [http://developer.android.com/guide/topics/connectivity/usb/host.html Android USB Page] for more in this) that we registered, '''mUsbReceiver''', looks like this:
 
The broadcast receiver (See the main [http://developer.android.com/guide/topics/connectivity/usb/host.html Android USB Page] for more in this) that we registered, '''mUsbReceiver''', looks like this:
  
<syntaxhighlight lang="Java">
+
<pre>
 
     private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
 
     private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
 
         public void onReceive(Context context, Intent intent) {
 
         public void onReceive(Context context, Intent intent) {
Line 161: Line 159:
 
                     }
 
                     }
 
                     else {
 
                     else {
                         Log.d("GrTemplateUSRP", "Permission denied for device " + device);
+
                         Log.d(&quot;GrTemplateUSRP&quot;, &quot;Permission denied for device &quot; + device);
 
                     }
 
                     }
 
                 }
 
                 }
 
             }
 
             }
 
         }
 
         }
     };</syntaxhighlight>
+
     };</pre>
 
If the device registers and looks good, we assign it to our class object '''mUsbDevice''' and launch the next stage of starting up our application by calling '''SetupUSB'''.
 
If the device registers and looks good, we assign it to our class object '''mUsbDevice''' and launch the next stage of starting up our application by calling '''SetupUSB'''.
  
<syntaxhighlight lang="Java">    private void SetupUSB() {
+
<pre>    private void SetupUSB() {
 
         final UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);
 
         final UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);
  
Line 175: Line 173:
 
             fd = connection.getFileDescriptor();
 
             fd = connection.getFileDescriptor();
 
         } else {
 
         } else {
             Log.d("GrTemplateUSRP", "Didn't get a USB Device Connection");
+
             Log.d(&quot;GrTemplateUSRP&quot;, &quot;Didn't get a USB Device Connection&quot;);
 
             finish();
 
             finish();
 
         }
 
         }
Line 182: Line 180:
 
             usbfs_path = properDeviceName(mUsbDevice.getDeviceName());
 
             usbfs_path = properDeviceName(mUsbDevice.getDeviceName());
 
         } else {
 
         } else {
             Log.d("GrTemplateUSRP", "Didn't get a USB Device");
+
             Log.d(&quot;GrTemplateUSRP&quot;, &quot;Didn't get a USB Device&quot;);
 
             finish();
 
             finish();
 
         }
 
         }
Line 189: Line 187:
 
         int pid = mUsbDevice.getProductId();
 
         int pid = mUsbDevice.getProductId();
  
         Log.d("GrTemplateUSRP", "Found fd: " + fd + " usbfs_path: " + usbfs_path);
+
         Log.d(&quot;GrTemplateUSRP&quot;, &quot;Found fd: &quot; + fd + &quot; usbfs_path: &quot; + usbfs_path);
         Log.d("GrTemplateUSRP", "Found vid: " + vid + " pid: " + pid);
+
         Log.d(&quot;GrTemplateUSRP&quot;, &quot;Found vid: &quot; + vid + &quot; pid: &quot; + pid);
  
 
         StartRadio();
 
         StartRadio();
     }</syntaxhighlight>
+
     }</pre>
 
This function gets information from the USB device, prints it to logcat, and then calls '''StartRadio'''. We use a parsing function called '''properDeviceName''' to extract the USB device path from the device itself:
 
This function gets information from the USB device, prints it to logcat, and then calls '''StartRadio'''. We use a parsing function called '''properDeviceName''' to extract the USB device path from the device itself:
  
<syntaxhighlight lang="Java">    public final static String properDeviceName(String deviceName) {
+
<pre>    public final static String properDeviceName(String deviceName) {
 
         if (deviceName == null) return DEFAULT_USBFS_PATH;
 
         if (deviceName == null) return DEFAULT_USBFS_PATH;
 
         deviceName = deviceName.trim();
 
         deviceName = deviceName.trim();
 
         if (deviceName.isEmpty()) return DEFAULT_USBFS_PATH;
 
         if (deviceName.isEmpty()) return DEFAULT_USBFS_PATH;
  
         final String[] paths = deviceName.split("/");
+
         final String[] paths = deviceName.split(&quot;/&quot;);
 
         final StringBuilder sb = new StringBuilder();
 
         final StringBuilder sb = new StringBuilder();
 
         for (int i = 0; i < paths.length - 2; i++)
 
         for (int i = 0; i < paths.length - 2; i++)
Line 207: Line 205:
 
                 sb.append(paths[i]);
 
                 sb.append(paths[i]);
 
             else
 
             else
                 sb.append("/").append(paths[i]);
+
                 sb.append(&quot;/&quot;).append(paths[i]);
 
         final String stripped_name = sb.toString().trim();
 
         final String stripped_name = sb.toString().trim();
 
         if (stripped_name.isEmpty())
 
         if (stripped_name.isEmpty())
Line 213: Line 211:
 
         else
 
         else
 
             return stripped_name;
 
             return stripped_name;
     }</syntaxhighlight>
+
     }</pre>
This always seems to be "/dev/bus/usb", but we get it here each time in case something else changes.
+
This always seems to be &quot;/dev/bus/usb&quot;, but we get it here each time in case something else changes.
  
 
Finally, we call '''StartRadio''', which is really the rest of the '''OnCreate''' stuff we have done previously, but now we're waiting until the USB device is properly assigned.
 
Finally, we call '''StartRadio''', which is really the rest of the '''OnCreate''' stuff we have done previously, but now we're waiting until the USB device is properly assigned.

Please note that all contributions to GNU Radio are considered to be released under the Creative Commons Attribution-ShareAlike (see GNU Radio:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel | Editing help (opens in new window)