导航:首页 > 操作系统 > libusbandroid

libusbandroid

发布时间:2024-05-14 12:42:56

⑴ 如何在android手机上使用hackrf

JNI方面
1)将编译好的库导入工程
拷贝libhackrf.h、libhackrf.so和libusb1.0.so到工程目录,创建libusb1.0.mk和libhackrf.mk
libusb1.0.mk
通过libhackrf.mk即可将libhackrf连接到工程中。
2)初始化hackrf,注册接收函数。
JNI接口
[cpp] view plain
JNIEXPORT jstring JNICALL java_com_pagekpang_hackrftouch_HackRFTouch_test(
JNIEnv *env, jclass cls) {
int result = HACKRF_SUCCESS;
result = hackrf_init();
if (result == HACKRF_SUCCESS) {
return env->NewStringUTF("OK!");
} else {
return env->NewStringUTF("Error!");
}
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_init(
JNIEnv *env, jobject thiz) {
int result = HACKRF_SUCCESS;
uint8_t board_id = BOARD_ID_INVALID;
char pbuf[2048] = { 0 };
result = hackrf_init();
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_init() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}

result = hackrf_open(&device);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_open() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}

result = hackrf_board_id_read(device, &board_id);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_board_id_read() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
sprintf(pbuf, "Board ID Number: %d (%s)\n", board_id,

hackrf_board_id_name((hackrf_board_id) board_id));
return env->NewStringUTF(pbuf);
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setSampleRateManual(

JNIEnv *env, jobject thiz, jlong freq, jint divi) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_sample_rate_manual(device, freq, divi);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_sample_rate_manual() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setVgaGain(
JNIEnv *env, jobject thiz, jint vga) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_vga_gain(device, vga);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_vga_gain() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setLnaGain(
JNIEnv *env, jobject thiz, jint lna) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_lna_gain(device, lna);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_lna_gain() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setFreq(
JNIEnv *env, jobject thiz, jlong freq) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_freq(device, freq);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_freq() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_setAmpEnable(

JNIEnv *env, jobject thiz, jint b) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_set_amp_enable(device, b == 0 ? false : true);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_set_amp_enable() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}

JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_startRX(
JNIEnv *env, jobject thiz) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_start_rx(device, hackrf_rx_cb, env->NewGlobalRef(thiz));
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_start_rx() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
JNIEXPORT jstring JNICALL Java_com_pagekpang_hackrftouch_HackRFTouch_stopRX(

JNIEnv *env, jobject thiz) {
int result = HACKRF_SUCCESS;
char pbuf[2048] = { 0 };
result = hackrf_stop_rx(device);
if (result != HACKRF_SUCCESS) {
sprintf(pbuf, "hackrf_stop_rx() failed: %s (%d)\n",
hackrf_error_name((hackrf_error) result), result);
return env->NewStringUTF(pbuf);
}
return env->NewStringUTF("ok");
}
java层与JNI接口的绑定

[java] view plain
package com.pagekpang.hackrftouch;

public class HackRFTouch {

static {
System.loadLibrary("usb1.0");
System.loadLibrary("hackrf");
System.loadLibrary("HackrfTouch");
}

public static native String test();

private native String init();

private native String setSampleRateManual(long freq, int divi);

private native String setVgaGain(int g);

private native String setLnaGain(int g);

private native String setFreq(long freq);

private native String setAmpEnable(int f);

public native String startRX();

public native String stopRX();

public native float[] readRx();

private String retString = "";
private Boolean isOpen = false;

private ReadRxThread mReadRxThread = null;

public HackRFTouch() {
// TODO Auto-generated constructor stub
retString = init();
if (!retString.contains("failed")) {
isOpen = true;
mReadRxThread = new ReadRxThread(this);
}
}

class ReadRxThread extends Thread {
HackRFTouch mThisHackRFTouch = null;

public ReadRxThread(HackRFTouch t) {
// TODO Auto-generated constructor stub
mThisHackRFTouch = t;
}

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while(true){
mThisHackRFTouch.cb(mThisHackRFTouch.readRx());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public Boolean isOpen() {
return isOpen;
}

public String getLastError() {
return retString;
}

public Boolean HsetSampleRateManual(long freq, int divi) {
if (!isOpen) {
return false;
}
retString = setSampleRateManual(freq, divi);
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

public Boolean HsetLnaGain(int g) {
if (!isOpen) {
return false;
}
retString = setLnaGain(g);
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

public Boolean HsetVgaGain(int g) {
if (!isOpen) {
return false;
}
retString = setVgaGain(g);
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

public Boolean HsetFreq(long freq) {
if (!isOpen) {
return false;
}
retString = setFreq(freq);
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

public Boolean HstopRX() {
if (!isOpen) {
return false;
}
retString = stopRX();
if (retString.contains("failed")) {
return false;
} else {
return true;
}
}

3)FFT运算

[cpp] view plain
if (device != NULL) {
float raw[1024];
int dalen = transfer->valid_length;
float realF, imagF, maxF;
uint8_t *pbuf = transfer->buffer;
while (dalen > 0 && runcount == 0) {
complex<double>* fdata = new complex<double> [1024];
complex<double>* fout = new complex<double> [1024];
for (int i = 0; i < 2048; i += 2) {
fdata[i / 2] = complex<double>(meanN(&pbuf[i], 100), meanN(&pbuf[i + 1], 100));
}
FFT(fdata, fout, 10);
//dft(fdata, 10, 0);
//fout = fdata;
maxF = 0.0;
for (int i = 0; i < 1024; i++) {
raw[i] = pow(pow(fout[i].real(), 2) + pow(fout[i].imag(), 2),
0.5);
if (maxF < raw[i]) {
maxF = raw[i];
}
}

for (int i = 0; i < 1024; i++) {
raw[i] = raw[i] / maxF;
}

sendBuf(raw);
//send(g_client, (char *)&raw, 4*1024, 0); //发送数据
dalen -= 2048;
pbuf += 2048;
runcount = 2;
}
runcount--;
//printf("E");
} else {
printf("O");
}

阅读全文

与libusbandroid相关的资料

热点内容
速腾汽车怎么给安卓手机充电 浏览:270
苹果安卓换机用什么软件好 浏览:261
华为通话加密电话 浏览:60
什么服务器可以代替计算机 浏览:962
服务器当电脑用怎么安装显卡 浏览:521
回女友消息神器python 浏览:52
App分类框怎么添加 浏览:546
海尔帅王子冰箱压缩机启动跳闸 浏览:488
cadfill命令什么意思 浏览:336
中公专项题库pdf 浏览:933
拼多多缓存在哪个文件夹 浏览:643
安卓无卡汽车导航怎么升级 浏览:50
cs管理员命令是什么 浏览:126
华为v10模拟加密门禁卡 浏览:725
android修改app名称 浏览:640
辐射3伙伴命令 浏览:247
苹果电脑pdf转换成word 浏览:921
纯小数运算法则 浏览:385
php什么是事务 浏览:859
怎么添加网名文件夹 浏览:771